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

使用Python(MSAL)通过客户端密钥连接OneDrive时遇权限不足错误的求助

使用Python(MSAL)通过客户端密钥连接OneDrive时遇权限不足错误的求助

我现在想通过客户端密钥的方式连接OneDrive,读取共享给我账号的文件——这么做是为了让代码可以被定时执行。我已经在Azure里给用于连接API的应用设置了对应的API权限,但还是遇到了错误:Insufficient privileges to complete the operation。

我Python水平属于中等水平,实在搞不定这个问题,希望能得到大家的帮助!

下面是我用来连接OneDrive的示例代码:

from msal import ConfidentialClientApplication
import requests

# App credentials
CLIENT_ID = "your_client_id"         # Application (Client) ID from Azure
CLIENT_SECRET = "your_client_secret" # Generated client secret
TENANT_ID = "your_tenant_id"         # Directory (Tenant) ID from Azure

# MSAL application
app = ConfidentialClientApplication(
    client_id=CLIENT_ID,
    client_credential=CLIENT_SECRET,
    authority=f"https://login.microsoftonline.com/{TENANT_ID}",
)

# Get a token for Microsoft Graph
SCOPES = ["https://graph.microsoft.com/.default"]  # Required for Client Credentials Flow
token_response = app.acquire_token_for_client(scopes=SCOPES)

# Check token response
if "access_token" in token_response:
    access_token = token_response["access_token"]
    print("Access token acquired.")

    # Use the token to call Microsoft Graph
    GRAPH_API_URL = "https://graph.microsoft.com/v1.0/users"
    headers = {"Authorization": f"Bearer {access_token}"}

    response = requests.get(GRAPH_API_URL, headers=headers)

    if response.status_code == 200:
        print("Graph API response:", response.json())
    else:
        print("Graph API error:", response.json())
else:
    print("Failed to acquire token:", token_response.get("error_description"))

备注:内容来源于stack exchange,提问作者Nina

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 03:19:43