【问题标题】:Load files from SharePoint in Python在 Python 中从 SharePoint 加载文件
【发布时间】:2023-03-06 04:35:02
【问题描述】:

所以我正在尝试使用以下代码加载位于 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://login.microsoftonline.com/extSTS.srf 获取二进制安全令牌 从 https://DOMAIN.website/siteDocuments/Document/example/ 检索身份验证 cookie 时出错

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

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

【问题讨论】:

    标签: python rest authentication sharepoint


    【解决方案1】:

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

    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())

    结果:

    【讨论】:

    • ModuleNotFoundError: No module named 'office365.runtime.utilities.request_options'
    • request_options 已移至:from office365.runtime.http.request_options import RequestOptions
    猜你喜欢
    • 2019-05-09
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    相关资源
    最近更新 更多