你好呀,
我正在使用2.1.7.post1版本(根据 pip freeze),无需 MFA 即可登录 sharepoint。
我目前正在尝试从公司的共享点获取一个 excel 文件,其中包含以下脚本(根据项目的 README 构建):
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
url = 'https://mycompany.sharepoint.com/sites/documentssite/'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web title: {0}".format(web.properties['Title']))
运行此脚本提供以下输出:
An error occurred while retrieving auth cookies from https://mycompany.sharepoint.com/sites/documentssite//_vti_bin/idcrl.svc
我注意到通过在调试模式下运行这个脚本,问题来自于类SamlTokenProvider
的方法acquire_authentication_cookie
SamlTokenProvider
实际上,该方法是使用federated=True
调用的,因此 HTTP 请求是在https://mycompany.sharepoint.com/sites/documentssite//_vti_bin/idcrl.svc 上使用 HEAD 方法完成的。 您可以在失败的部分下方找到:
def acquire_authentication_cookie(self, security_token, federated=False):
...
else:
self._auth_cookies['SPOIDCRL'] = None
session.head(self.__sts_profile.signInPageUrl,
headers={
'User-Agent': 'Office365 Python Client',
'X-IDCRL_ACCEPTED': 't',
'Authorization': 'BPOSIDCRL {0}'.format(security_token),
'Content-Type': 'application/x-www-form-urlencoded'
})
logger.debug_secrets("session.cookies: %s", session.cookies)
cookies = requests.utils.dict_from_cookiejar(session.cookies)
logger.debug_secrets("cookies: %s", cookies)
if not cookies:
self.error = "An error occurred while retrieving auth cookies from {0}".format(self.__sts_profile.signInPageUrl)
logger.error(self.error)
return False
响应不包含任何 cookie,这就是我收到消息错误的原因。
顺便说一句,当我将 url 设置为“ https://mycompany.sharepoint.com ”时,我遇到了同样的问题。
如果我没有提供有关该问题的足够详细信息,请告诉我。
先感谢您 !
关于这个问题的一些新闻:
当我使用 curl 向https://mycompany.sharepoint.com/sites/documentssite//_vti_bin/idcrl.svc发出 HEAD 请求时,我被重定向到https://mycompany.sharepoint.com/_vti_bin/idcrl.svc/ (响应代码:307)。
所以我向这个链接发出了一个 HEAD 请求,我得到了一个 Method Not Allowed 响应......
我猜这个问题来自我公司方面..
问候!
感谢您提供的详细信息!
2.1.7.post1
目前有点过时,请您再试一次,但使用 _latest_ 版本(目前2.1.10.1
),可以通过 GitHub 下载:
pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
或来自替代索引
从那时起,在身份验证方面已经解决了一些错误修复。
我安装了最后一个版本,它工作正常! (实际上解决它很简单!:))
谢谢你的帮助:D