gpt4 book ai didi

python - 使用图形 api 调用 onedrive 方法

转载 作者:行者123 更新时间:2023-12-02 08:32:13 24 4
gpt4 key购买 nike

我购买了 MS Office,他们在 Onedrive 上为我提供了一个空间。现在我想通过我的 python 控制台应用程序中的 ms graph API 进行操作(我之前使用过 python onedrivesdk,但现在据说它已被弃用)。

我已经注册了一个应用程序,并且可以通过 Portal.azure.com 上的 Azure AD 查看它。

目前我正在尝试像这样与我的 onedrive 交互:

tenant_id = 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
authority = f'https://login.microsoftonline.com/{tenant_id}'
scope = 'https://graph.microsoft.com/.default'

app = msal.ConfidentialClientApplication(self.client_id, authority=authority, client_credential=self.client_secret)
result = app.acquire_token_silent(["https://graph.microsoft.com/.default"], account=None)
if not result:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])

authToken = result['access_token']
#### Make a call to the graph API
graphResponse = requests.get('https://graph.microsoft.com/beta/me/drive',headers={'Authorization':f'Bearer {authToken}'})
if graphResponse.status_code != 200:
print('Error code: ', graphResponse.status_code)
print(graphResponse.text)

我成功获得了访问 token ,但是当我尝试调用 /me/drive

我得到 status_code = 400

Current authenticated context is not valid for this request. This occurs when a request is made to an endpoint that requires user sign-in. For example, /me requires a signed-in user. Acquire a token on behalf of a user to make requests to these endpoints. Use the OAuth 2.0 authorization code flow for mobile and native apps and the OAuth 2.0 implicit flow for single-page web apps

我已通过“API 权限 -> 添加权限”在门户上添加应用程序的权限,但我无法授予管理员同意(在另一个 MS 帐户中,我拥有完整的 azure我是管理员的订阅)因为我不是管理员。但是我的 MS Office 分配给的这个帐户的管理员是谁?

最佳答案

根据您提供的代码,您使用OAuth 2.0 client credentials flow完成 Azure AD 身份验证并获取访问 token 。服务主体所需的访问 token 。我们无法使用访问 token 来调用 /me/drive端点。我们可以使用访问 token 来调用 /users/<UserObjectIdOrUserPrincipalName}>/drive端点。更多详情请引用document

如果您想调用 /me/drive端点,我建议您使用 OAuth 2.0 authorization code flow 。关于如何在您的应用程序中实现,请引用sample .

关于python - 使用图形 api 调用 onedrive 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59868117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com