如何解决读取日历或发送邮件时 Microsoft Graph 返回“NoPermissionsInAccessToken”的问题

2024-01-06

在我们部门内,我们有一个经典 ASP 网站,该网站一直使用我们的公司 Google 帐户进行身份验证并将事件插入我们的 Google 日历。该组织刚刚从 Google 切换到 Office 365,我尝试将代码切换为使用 Outlook - 但没有成功。

我可以使用访问令牌来获取https://graph.microsoft.com/v1.0/me并查看基本配置文件,但如果我尝试阅读日历事件或发送电子邮件,我会看到以下消息:

"error": {
  "code": "NoPermissionsInAccessToken",
  "message": "The token contains no permissions, or permissions can not be understood."

我使用我的个人 Microsoft 帐户创建了一个 Azure 帐户并添加了一个应用程序。我将我的同事和我的公司帐户添加为访客用户。我设置了所需的权限并进行了管理员同意:

然后我按照以下步骤操作这一页 https://developer.microsoft.com/en-us/graph/graph/docs/concepts/auth-v2-user.

我第一次访问https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/authorize,包括以下查询项:

client_id={client id}
response_type=code
redirect_uri={our URL}
response_mode=query
scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
state={number}

这会返回一个“代码”,我将其发送到https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/token, 包括:

grant_type=authorization_code
client_secret={client secret}
client_id={client id}
scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
state={same number as above}
redirect_uri={same URL as above}
code={code returned from /authorize}

这将返回 id、访问令牌和刷新令牌。如果我使用解码访问令牌https://jwt.ms/ https://jwt.ms/,它似乎包含适当的受众和范围:

"aud": "https://graph.microsoft.com"
"scp": "Calendars.ReadWrite email Mail.Send openid profile User.Read"

...尽管我注意到没有“角色”条目,我在一些文档中看到了这一点。这是一个问题吗?

然后我将访问令牌发送到https://graph.microsoft.com/v1.0/me具有以下标题:

"Authorization", "Bearer {access token}"
"Host", "graph.microsoft.com"
"Content-Type", "application/json"
"Prefer", "outlook.timezone Europe/London"

这将返回我的电子邮件地址等,但如果我将 URL 更改为其他任何内容,例如/me/photo/$value, /me/calendar/events,或者尝试发送一封电子邮件,我被告知令牌中没有权限。

我看到还有其他类似的问题,但主要是针对client_credentials流程(我不确定这有什么区别),并且没有一个答案可以帮助我解决我的问题。

如果我明显错过了任何东西,或者我以错误的方式访问客户端的图形数据,有人可以告诉我吗?对于 Google 来说,这似乎很简单,但我发现 Microsoft 文档和示例有点不太详细。


添加访客用户是没有意义的。

您所做的就是管理员使用您的个人 Microsoft 帐户同意 Azure AD。

您实际需要做的是目标 Azure AD/O365 租户的管理员同意。

构建同意链接如下:

https://login.windows.net/{tenant ID of the target Azure AD}/oauth2/authorize?response_type=id_token&client_id={client ID}&redirect_uri={reply url}&response_mode=form_post&nonce=a4014117-28aa-47ec-abfb-f377be1d3cf5&resource=https://graph.microsoft.com&prompt=admin_consent

在浏览器中访问它并使用目标 Azure AD 的管理员帐户登录。

另一个问题是您正在使用代表用户获取访问权限 https://developer.microsoft.com/en-us/graph/graph/docs/concepts/auth-v2-user。因此,您必须在 Azure AD 应用程序中分配委派权限而不是应用程序权限。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何解决读取日历或发送邮件时 Microsoft Graph 返回“NoPermissionsInAccessToken”的问题 的相关文章

随机推荐