使用python进行Sharepoint身份验证

时间:2019-04-26 15:46:51

标签: python authentication sharepoint request

我正在尝试使用python下载一个Excel文件,该文件托管在 Microsoft Azure平台的共享点中。我尝试通过使用HTTPforhumans的request来检索文件:

r = requests.get(url)

但是我的请求一直被拒绝(r.status_code返回200),因为在尝试访问该文件之前,我需要登录到有效帐户。我确实有一个有效的帐户和密码,并且可以通过浏览器访问我的帐户和excel文件。但是我不知道如何处理Azure身份验证过程。显然,这并不像做那么简单:

auth = HTTPBasicAuth('email@somewhere.com', 'pass1234')
r = requests.post(url=url, auth=auth)

我的理解是,要遵循流程,但是当我尝试阅读文档时,它就让我感到头疼(我是一名工程师,在这种环境下没有经验)。

有人可以指导我如何登录和下载文件吗?

1 个答案:

答案 0 :(得分:1)

尝试O365 rest python client library。它支持SharePoint Online authentication,并允许下载/上传文件,如下所示:请在此处找到代码:

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username,password)   
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(context, "/Shared Documents/User Guide.docx")
with open("./User Guide.docx", "wb") as local_file:
    local_file.write(response.content)

您可以使用以下命令下载最新版本

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git

有关更多参考,请访问link

希望有帮助。

相关问题