gpt4 book ai didi

python - 使用python下载sharepoint 365文档

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:26 27 4
gpt4 key购买 nike

我正在尝试使用 Python 自动下载 sharepoint Office 365 文件。我找到了以下客户 https://github.com/vgrem/Office365-REST-Python-Client

但是,我的要求是使用一个简单的客户端,该客户端使用 clientid 和 key 来获取访问 token ,然后下载文件。

如果您了解 OAuth2,那么此流程称为客户端凭据授予。但是当我浏览示例部分中的 Python 客户端时,我看到

settings.py
settings = {
'url': 'https://mediadev20.sharepoint.com/sites/contoso',
'username': 'mattim@mediadev20.onmicrosoft.com',
'password': 'P@ssw0rd'
}

app_settings = {
'url': 'https://mediadev20.sharepoint.com/sites/contoso',
'client_id': '99cbd1a9-ec8d-4e89-96c3-699993089d65',
'client_secret': 'VMdT8mOurDhsvG8yDnP3yFg',
'redirect_url': 'https://github.com/vgrem/Office365-REST-Python-Client/'
}

我的问题是我不知道如何使用redirect_url,因为我的是一个独立的客户端,并且没有这样的redirect_url。

有人有示例代码或指针告诉我如何在不使用redirect_url的情况下使用该库吗?

最佳答案

对于这种情况,您可以考虑通过 SharePoint App-Only 授予访问权限流动。在最新版本的库中,引入了对 SharePoint App-Only 访问的支持:

AuthenticationContext.acquire_token_for_app(client_id,client_secret)

关注this article关于如何使用 SharePoint App-Only 授予访问权限,下面我将总结要点:

  • 首先,您需要setup your app-only principal
  • 下一步是向新创建的主体授予权限
  • 创建主体并获得同意后,您可以使用请求访问的主体 ID 和 key 如下所示:

示例:

from office365.runtime.auth.authentication_context import AuthenticationContext

app_settings = {
'url': 'https://contoso.sharepoint.com/',
'client_id': '8efc226b-ba3b-4def-a195-4acdb8d20ca9',
'client_secret': '',
}

context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])

ctx = ClientContext(app_settings['url'], context_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))

关于python - 使用python下载sharepoint 365文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com