使用图形API调用onedrive方法

问题描述 投票:0回答:1

我已经购买了MS Office,他们给我一个驱动器上的空间。现在,我想通过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)

我成功获取了访问令牌,但是当我尝试致电/ me / drive

我得到状态码= 400,并带有

当前认证的上下文对此请求无效。这个向要求用户的端点发出请求时发生登入。例如,/ me需要登录的用户。获取令牌代表用户向这些端点发出请求。使用适用于移动和本地应用的OAuth 2.0授权代码流以及单页网络应用的OAuth 2.0隐式流程

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

python azure onedrive azure-ad-graph-api
1个回答
0
投票

根据您提供的代码,您使用OAuth 2.0 client credentials flow完成Azure AD身份验证并获取访问令牌。服务主体要求的访问令牌。我们无法使用访问令牌来调用/me/drive端点。我们只是可以使用访问令牌来调用/users/<UserObjectIdOrUserPrincipalName}>/drive端点。有关更多详细信息,请参阅document

因此,如果您要呼叫/me/drive端点,建议您使用OAuth 2.0 authorization code flow。关于如何在您的应用程序中实现它,请参考sample

© www.soinside.com 2019 - 2024. All rights reserved.