Microsoft Graph returns 401 即使我有有效的访问令牌

Microsoft Graph returns 401 even if I have a valid access token

目前我正在尝试获取所有 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>

谢谢

Invalid audience 表示您拥有的令牌是为 API 而不是您正在呼叫的那个发出的。如果您复制令牌并转到 https://jwt.ms,您可以解析它并检查 aud 声明。如果不是 https://graph.microsoft.com,则不能使用它来调用 Microsoft Graph。