即使我有有效的访问令牌,Microsoft Graph也会返回401

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

当前,我正在尝试获取所有Outlook用户日历事件。我已经按照所有说明设置了Azure Active Directory V 2.0,并且正在获取访问令牌:

Office.context.auth.getAccessTokenAsync(
    {
      allowConsentPrompt: true,
      allowSignInPrompt: true,
    },
   (result) => {
      if (result.status === 'succeeded') {
        return result.value
      }

      return result.error
    },
  )

此后,我试图通过以下方式获取用户日历事件:

fetch(
`https://graph.microsoft.com/v1.0/me/events`,
{
  method: 'GET',
  headers: {
    Authorization: `Bearer ${accessToken}`,
    'Access-Control-Allow-Credentials': true,
    'access-control-allow-origin': 'my.domain',
    'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
    Prefer: 'outlook.timezone',
  }
}

最后,我收到带有正文的响应401:

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure. Invalid audience.",
    "innerError": {
      "request-id": "1fba5937-3106-460c-98a6-a1e7858b8116",
      "date": "2020-02-12T13:59:21"
    }
  }
}

我目前处于困境,我还尝试使用而不是graph.microsoft.com来使用Office.context.mailbox.restUrl,但是该人不接受我拥有的访问令牌。我可能会跳过一些明显的事情吗?

PS:我忘了提到我赋予外接程序的范围权限:

<Scopes>
      <Scope>user.read</Scope>
      <Scope>profile</Scope>
      <Scope>openid</Scope>
      <Scope>email</Scope>
      <Scope>offline_access</Scope>
      <Scope>files.read.all</Scope>
      <Scope>calendars.read</Scope>
    </Scopes>

谢谢

javascript microsoft-graph office-js outlook-web-addins
1个回答
1
投票

Invalid audience表示已为您正在调用的API以外的API发行了您的令牌。如果您复制令牌并转到https://jwt.ms,则可以对其进行解析并检查aud声明。如果不是https://graph.microsoft.com,则无法使用它来调用Microsoft Graph。

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