我指的是this medium blog和 the official document from Microsoft使用 Azure 应用程序通过图形 API 访问一个驱动器。我使用以下 node.js 代码来获取 Microsoft Graph API 的访问 token :
const axios = require('axios');
const qs = require('qs');
const postData = {
client_id: client_id from azure app,
scope: 'https://graph.microsoft.com/.default',
client_secret: app_secret from azure app
grant_type: 'client_credentials'
};
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
try{
let res = await axios.post('https://login.microsoftonline.com/{tenant from azure app}/oauth2/v2.0/token', qs.stringify(postData));
console.log(res.data);
}catch (e) {
console.log(e.message);
}
上面的代码返回以下响应:
{
token_type: 'Bearer',
expires_in: 3599,
ext_expires_in: 3599,
access_token: 'value of access token'
}
在执行上述步骤之前一切正常。我从上述响应中获取访问 token ,并尝试执行以下图形 API 来访问一个驱动器:
let config = { headers:
{'Authorization': "Bearer "+ res.data.access_token} # Obtain access token from the above response
};
let res_drive = await axios.get("https://graph.microsoft.com/v1.0/me/drive/sharedWithMe", config);
但是,上面的图形 API 调用会返回以下响应:
{
"error": {
"code": "BadRequest",
"message": "Unable to retrieve user's mysite URL.",
"innerError": {
"date": "2020-07-02T14:08:36",
"request-id": "67ef24fa-XXXX-XXXX-853a-XXXXXXXXXXXX"
}
}
}
我还设置了使用 Azure 应用程序访问图形 API 的权限,如下所示:
如果我遗漏了什么,请告诉我。
如何使用 Azure 应用程序通过图形 API 访问一个驱动器?
最佳答案
您的示例中获取 token 的方式对应于 OAuth client credentials grant flow (应用程序权限)。此流程中没有可用的 me
上下文(它只是登录用户的别名),因为不存在登录用户 em> 在此流程中,这就是首先发生所提供的错误的原因。
要在此流程中调用共享文件
端点,用户需要是 explicitly provided :
https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/drive/sharedWithMe
关于node.js - 无法使用 Azure 应用程序通过图形 API 访问一个驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62698690/