gpt4 book ai didi

python - 在 Python 中从 SharePoint 加载文件

转载 作者:行者123 更新时间:2023-12-05 03:53:55 27 4
gpt4 key购买 nike

所以我尝试使用这段代码加载位于 Python 中(子)目录中的文件:

import json

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
request = ClientRequest(ctx_auth)
options = RequestOptions("{0}/_api/web/".format(url))
options.set_header('Accept', 'application/json')
options.set_header('Content-Type', 'application/json')
data = request.execute_request_direct(options)
s = json.loads(data.content)
web_title = s['Title']
print("Web title: " + web_title)
else:
print(ctx_auth.get_last_error())

但是我收到以下错误:

Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf An error occurred while retrieving auth cookies from https:/DOMAIN.website/siteDocuments/Document/example/

现在 Microsoft 链接说端点只接受 POST 请求。收到 GET 请求。

有什么解决方法吗?谢谢。

最佳答案

我在我的在线 SP 上测试了这段代码。请修改您的代码如下:

import json

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions

tenant_url= "https://{tenant}.sharepoint.com"
ctx_auth = AuthenticationContext(tenant_url)

site_url="https://{tenant}.sharepoint.com/sites/{yoursite}"

if ctx_auth.acquire_token_for_user("username","password"):
request = ClientRequest(ctx_auth)
options = RequestOptions("{0}/_api/web/".format(site_url))
options.set_header('Accept', 'application/json')
options.set_header('Content-Type', 'application/json')
data = request.execute_request_direct(options)
s = json.loads(data.content)
web_title = s['Title']
print("Web title: " + web_title)
else:
print(ctx_auth.get_last_error())

结果: enter image description here

关于python - 在 Python 中从 SharePoint 加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61388095/

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