如何解决“NoPermissionsInAccessToken"阅读日历或发送邮件时由 Microsoft Graph 返回 [英] How to solve "NoPermissionsInAccessToken" returned by Microsoft Graph when reading calendar or sending mail

查看:27
本文介绍了如何解决“NoPermissionsInAccessToken"阅读日历或发送邮件时由 Microsoft Graph 返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Within our department we have a Classic ASP website that has been using our corporate Google accounts for authentication and to insert events into our Google Calendars. The organisation has just switched from Google to Office 365 and I'm trying to switch the code to use Outlook instead - with no success.

I can use the access token to GET https://graph.microsoft.com/v1.0/me and see the basic profile, but if I try to read calendar events or send an e-mail I see the message:

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

I created an Azure account with my personal Microsoft account and added an application. I added my colleagues and my corporate account as guest users. I set the permissions required and did the admin consent thing:

I then followed the steps on this page.

I first visit https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/authorize, including the following query items:

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}

This returns a "code", which I send to https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/token, including:

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}

This returns the id, access and refresh tokens. If I decode the access token using https://jwt.ms/, it seems to contain the appropriate audience and scope:

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

...although I notice that there is no "roles" entry, which I see in some of the documentation. Is that an issue?

I then send the access token to https://graph.microsoft.com/v1.0/me with the following headers:

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

That will return my e-mail address, etc., but if I change the URL to anything else, such as /me/photo/$value, /me/calendar/events, or try to send an e-mail I'm told that there are no permissions in the token.

I see that there are other similar questions, but they are mostly for the client_credentials flow (I'm not sure that makes any difference), and none of the answers has helped me resolve my problem.

Can someone please let me know if I've obviously missed anything - or I'm going about this in the wrong way to access Graph data on the client side? It seemed so straightforward with Google, but I'm finding the Microsoft documentation and examples a bit less detailed.

解决方案

Adding guest users is meaningless.

What you have done is Admin consent for the Azure AD with your personal Microsoft account.

What you actually need to do is Admin consent for the target Azure AD/ O365 tenant.

Construct a consent link as following:

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

Access it in a browser and log in with an admin account of target Azure AD.

Another issue is that you are using Get access on behalf of a user. So you have to assign Delegated permissions rather than Application permissions in the Azure AD app.

这篇关于如何解决“NoPermissionsInAccessToken"阅读日历或发送邮件时由 Microsoft Graph 返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆