You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.6环境下访问SharePoint遇office365库报错,求替代方案

无需升级Python 3.6访问SharePoint文件的替代方案

方案1:使用兼容Python 3.6的旧版office365库

新版office365-rest-python-client依赖Python 3.7+特性,导致导入报错。可安装兼容Python 3.6的旧版本:

pip install office365-rest-python-client==2.3.10

示例代码(用户名密码认证下载文件):

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.credentials import UserCredential

# 配置信息
site_url = "https://your-sharepoint-site-url"
username = "your-username"
password = "your-password"
file_url = "/sites/your-site/Shared Documents/example.docx"
save_path = "./example.docx"

# 建立连接并下载文件
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
file = ctx.web.get_file_by_server_relative_url(file_url)
with open(save_path, "wb") as f:
    f.write(file.read())

方案2:直接调用SharePoint REST API(无第三方库依赖)

用Python 3.6原生支持的requests库直接调用SharePoint REST接口,无需额外依赖。

步骤1:获取访问令牌(用户名密码方式)

import requests

# 认证参数
tenant_id = "your-tenant-id"
client_id = "your-client-id"  # 需在Azure AD注册应用并授予SharePoint权限
username = "your-username"
password = "your-password"

# 请求令牌
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
payload = {
    "grant_type": "password",
    "client_id": client_id,
    "resource": "https://your-sharepoint-site-url",
    "username": username,
    "password": password,
    "scope": "openid"
}
response = requests.post(token_url, data=payload)
access_token = response.json()["access_token"]

步骤2:调用API下载文件

site_url = "https://your-sharepoint-site-url"
file_relative_url = "/sites/your-site/Shared Documents/example.docx"
api_url = f"{site_url}/_api/web/getfilebyserverrelativeurl('{file_relative_url}')/$value"

headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(api_url, headers=headers)

# 保存文件到本地
with open("./example.docx", "wb") as f:
    f.write(response.content)

方案3:Azure Function中使用MSAL获取令牌

Azure Function环境下,用支持Python 3.6的msal库获取令牌,再调用SharePoint API:

import msal
import requests

def main(req):
    # MSAL配置
    client_id = "your-client-id"
    client_secret = "your-client-secret"
    tenant_id = "your-tenant-id"
    site_url = "https://your-sharepoint-site-url"
    file_path = "/sites/your-site/Shared Documents/example.docx"

    # 获取应用权限令牌
    authority = f"https://login.microsoftonline.com/{tenant_id}"
    app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
    scopes = [f"{site_url}/.default"]
    result = app.acquire_token_for_client(scopes=scopes)
    access_token = result["access_token"]

    # 下载文件
    api_url = f"{site_url}/_api/web/getfilebyserverrelativeurl('{file_path}')/$value"
    headers = {"Authorization": f"Bearer {access_token}"}
    response = requests.get(api_url, headers=headers)
    
    # 保存文件(示例为Azure Function本地路径,可替换为Blob存储等)
    with open("/home/site/wwwroot/example.docx", "wb") as f:
        f.write(response.content)
    return "File downloaded successfully"

注意事项

  • 应用权限方式(client credentials)需确保Azure AD应用已授予Sites.Read.All等对应权限,并在SharePoint站点中配置应用访问权限。
  • 用户名密码认证不推荐用于生产环境,优先使用应用权限或证书认证。

内容的提问来源于stack exchange,提问作者ra po

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 23:43:13