目前我正在尝试获取所有 Outlook 用户日历事件。我已按照所有说明设置 Azure Active Directory V 2.0,并且正在获取访问 token :
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"
}
}
}
我目前陷入困境,我还尝试使用 Office.context.mailbox.restUrl 而不是 graph.microsoft.com 来使用 Office.context.mailbox.restUrl,但该 URL 不接受我拥有的访问 token 。我可以跳过一些明显的事情吗?
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>
谢谢
最佳答案
无效受众
表示您拥有的 token 是为您所调用的 API 以外的 API 颁发的。如果您复制 token 并前往 https://jwt.ms ,您可以解析它并检查 aud
声明。如果不是 https://graph.microsoft.com
,则无法使用它来调用 Microsoft Graph。
关于javascript - 即使我有有效的访问 token ,Microsoft Graph 也会返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60190326/