无法使用 Azure 应用通过图表 API 访问一个驱动器

Can't access one drive through graph API using Azure app

我指的是 this medium blog and the official document from Microsoft 使用 Azure 应用程序通过图表 API 访问一个驱动器。我正在使用以下 node.js 代码获取 Microsoft Graph API 的访问令牌:

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);
}

以上代码returns响应如下:

{
token_type: 'Bearer',
expires_in: 3599,
ext_expires_in: 3599,
access_token: 'value of access 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 调用 returns 以下响应:

{
  "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 访问一个驱动器?

您的示例中获取令牌的方式对应于OAuth client credentials grant flow应用程序权限)。此流程中没有可用的 me 上下文(它只是 已登录用户 的别名),因为 不存在已登录用户 在此流程中,这就是提供的错误首先发生的原因。

要在此流程中调用 shared files 端点,用户需要 explicitly provided:

https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/drive/sharedWithMe