如何在 OAuth 2 请求中同时使用 Microsoft Graph 和 Office 365 范围?

How can I use both Microsoft Graph and Office 365 scopes in an OAuth 2 Request?

我正在向 https://login.microsoftonline.com/common/oauth2/v2.0/authorize 请求 OAuth2.0 令牌。我请求这些范围:

[
 'https://graph.microsoft.com/User.Read', 
 'https://graph.microsoft.com/Calendars.Read.Shared', 
 'offline_access', 
 'profile', 
 'email', 
 'https://outlook.office.com/mail.read'
]

我正在尝试获取 Microsoft Graph 范围和 Office 365 范围,但它给了我:

AADSTS70011: The provided value for the input parameter 'scope' is not valid. The scope https://graph.microsoft.com/User.Read https://graph.microsoft.com/Calendars.Read.Shared offline_access profile email https://outlook.office.com/mail.read is not valid

如果我取出 outlook.office.com 范围或两个 graph.microsoft.com 范围那么它就可以工作。

有没有办法同时访问两者?

v2.0 身份验证模型不支持在同一个请求中请求两个资源的权限,请尝试将请求分开,只要您获得每个资源的访问令牌,您就可以访问两个 API观众。

这是一种不同的方法,它允许您访问多个资源,只需一个登录请求(但访问令牌不同)。

根据使用的流程,刷新令牌 应该返回给您,它可以让您获得不同资源的访问令牌。

正在检索访问令牌

  1. 通过仅从一个资源(例如图形)请求范围来获取令牌 A(连同刷新令牌)。
  2. 使用 token refresh request 获取令牌 B,方法是仅从其他资源 (Office 365) 请求范围

在您的情况下,令牌刷新原始 HTTP 请求如下所示(授权代码流):

POST /common/oauth2/v2.0/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
&scope=https://outlook.office.com/mail.read
&refresh_token=...
&grant_type=refresh_token
&client_secret=...

上面的注释:为了便于阅读,在正文中添加了换行符。 scope 应该是 url 编码。

您现在有两个标记:适用于 Graph 的 A 和适用于 Office 365 的 B

需要多资源刷新令牌

只能使用多资源刷新令牌。您可以在 openid-configuration 中查看支持情况。要显示特定于租户的配置,请将 url 中的 common 替换为租户域。

microsoft_multi_refresh_token: OPTIONAL. A Boolean value that indicates whether the OpenID provider supports multi-resource refresh tokens, which are refresh tokens that can be redeemed for an access token for any resource registered with the AD FS server.

https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-oidce/586de7dd-3385-47c7-93a2-935d9e90441c