【问题标题】:Microsoft Graph API - AuthenticationError when accessing mailMicrosoft Graph API - 访问邮件时出现 AuthenticationError
【发布时间】:2020-07-15 10:51:13
【问题描述】:

我目前正在实施一个可以访问某些邮件字段的 Outlook 加载项。因此,我设置了一个启动项目并在 Microsoft Azure Active Directory 设置中设置了委派的 Mail.Read 权限,并将其添加到我的范围配置中。 但是,当我发送请求时

graph.microsoft.com/v1.0/me/messages/

我得到这个作为回应:

Error: Bad Request

at IncomingMessage.<anonymous> (C:\Users\zz\public\javascripts\odata-helper.js:53:29)
at IncomingMessage.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {                                                
code: 400,
bodyCode: 'AuthenticationError',
bodyMessage: 'Error authenticating with resource'
}

我不明白为什么会出现此错误,因为用户身份验证有效并且我对其他 Graph Endpoints 没有任何问题(例如,从 /me/drive/root/children 检索我的 OneDrive 文件列表)

我将其用作代码库: https://docs.microsoft.com/en-us/office/dev/add-ins/develop/create-sso-office-add-ins-nodejs

【问题讨论】:

  • 请提供请求或关联 ID 和时间戳。如果在异常中不可用,那么您可以让他们检查 fiddler 中的响应。
  • 嗨,您有机会查看我的答案吗?有用吗?

标签: rest azure-active-directory microsoft-graph-api outlook-addin microsoft-graph-mail


【解决方案1】:

我没有重现您的问题,graph.microsoft.com/v1.0/me/messages 在您共享的示例中运行良好。

请参考我的配置。

除了文档中提到的配置,我还做了如下改动。

在 Azure AD 应用中添加 Mail.Read 委派权限。

修改 app.js 文件中的 Microsoft Graph 端点。请注意,我正在查询消息的 subject 属性。于是我修改了两个地方:$select=subject&amp;$top=10itemNames.push(item['subject'])

app.get('/getuserdata', async function(req, res, next) {
  const graphToken = req.get('access_token');

  const graphData = await getGraphData(graphToken, "/me/messages", "?$select=subject&$top=10");

  if (graphData.code) {
      next(createError(graphData.code, "Microsoft Graph error " + JSON.stringify(graphData)));
  }
  else 
  {
    const itemNames = [];
    const oneDriveItems = graphData['value'];
    for (let item of oneDriveItems){
        itemNames.push(item['subject']);
    }
    res.send(itemNames)
  }
}

在加载项清单文件“manifest\manifest_local.xml”中,我添加了一个范围“Mail.Read”。

<WebApplicationInfo>  
    <Id>a7eb1d00-f724-4799-8a46-809f2dba63f8</Id>
    <Resource>api://localhost:44355/a7eb1d00-f724-4799-8a46-809f2dba63f8</Resource>
    <Scopes>  
      <Scope>Files.Read.All</Scope>
      <Scope>profile</Scope>
      <Scope>Mail.Read</Scope>
    </Scopes>  
</WebApplicationInfo> 

在文件routes\authRoute.js 中,我像这样添加“Mail.Read”:

  else {
    const [schema, jwt] = authorization.split(' ');
    const formParams = {
      client_id: process.env.CLIENT_ID,
      client_secret: process.env.CLIENT_SECRET,
      grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      assertion: jwt,
      requested_token_use: 'on_behalf_of',
      scope: ['Files.Read.All', 'Mail.Read'].join(' ')
    };

这些都是配置,我可以在Microsoft Word插件中获取消息的主题:

【讨论】:

  • 感谢您非常详细的回答,我合并了您的配置。然而,什么都没有改变。我仍然得到 AuthenticationError。有趣的是,我刚刚在 Graph Explorer 上尝试了相同的端点(使用我的公司凭据)并得到了相同的错误。整个事情可能与我使用本地 Exchange 实例的公司有关吗?或者这无关紧要?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多