用Python从SharePoint加载文件

2024-09-23 22:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试使用以下代码加载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请求

有什么解决办法吗?谢谢


Tags: fromimportauthwebjsonurltitlerequest
1条回答
网友
1楼 · 发布于 2024-09-23 22:25:21

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

&13; 第13部分,;
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())
和#13;
和#13;

结果: enter image description here

相关问题 更多 >