用Python从SharePoint加载文件

用Python从SharePoint加载文件,python,rest,authentication,sharepoint,Python,Rest,Authentication,Sharepoint,因此,我尝试使用以下代码加载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 RequestOpt

因此,我尝试使用以下代码加载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())
但我得到了以下错误:

无法从中获取的二进制安全令牌 从https:/DOMAIN.website/siteDocuments/Document/example检索身份验证cookie时出错/

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


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

我在我的SP online上测试了这段代码。请按以下方式修改您的代码:

导入json
从office365.runtime.auth.authentication\u上下文导入AuthenticationContext
从office365.runtime.client\u请求导入ClientRequest
从office365.runtime.utilities.request\u选项导入RequestOptions
租户_url=“https://{tenant}.sharepoint.com”
ctx\u auth=AuthenticationContext(租户\u url)
site_url=“https://{tenant}.sharepoint.com/sites/{yoursite}”
如果ctx授权为用户获取令牌(“用户名”、“密码”):
请求=ClientRequest(ctx_auth)
options=RequestOptions(“{0}/_-api/web/”。格式(站点url))
options.set_头('Accept','application/json')
options.set_头('Content-Type','application/json')
数据=请求。直接执行请求(选项)
s=json.loads(data.content)
web_title=s['title']
打印(“网页标题:”+网页标题)
其他:

打印(ctx_auth.get_last_error())
ModuleNotFoundError:没有名为'office365.runtime.utilities.request_options'的模块已移动到:from office365.runtime.http.request_options import RequestOptions