node.js - 无法使用 Azure 应用程序通过图形 API 访问一个驱动器

标签 node.js azure microsoft-graph-api onedrive

我指的是this medium blogthe 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 的权限,如下所示: enter image description here

如果我遗漏了什么,请告诉我。

如何使用 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/

相关文章:

javascript - 如何更新 Mongoose 中的数组元素 - MongoDB

linux - Azure AKS 添加具有不同 Linux 操作系统的节点

azure - 诊断/日志存储表在哪里?

android - 即使安装了 native Microsoft 应用程序,登录始终会打开 Web 对话框 [ms graph 如果可能,请尝试使用共享帐户登录]

microsoft-graph-api - 包含 Microsoft Graph 查询的过滤器不起作用

javascript - Node js console.log 没有显示任何内容

node.js - 为什么我的快速代码中的下一个函数无法正常工作?

azure-active-directory - 资源 <Random Guid> 不存在或其查询的引用属性对象之一不存在

javascript - 正则表达式 : Find out the name and state from a string in javascript

azure 订阅 "You are not eligible for an Azure subscription"