要获得与Office 365 API兼容的令牌需要设置什么范围和或资源?

问题描述 投票:2回答:1

我们已经在Azure AD中为我们的客户端应用程序获得了Office 365管理API的委派权限和应用程序ServiceHealth.Read权限的管理员同意。

如果我们要调用office365管理api,我们将无法确定令牌获取过程中的范围和资源。

是否为其直接令牌获取的client_credentials授予方法

authorization code然后用于登录用户方法的令牌

[client_credentials授予方法最好,但是如果必须通过身份验证代码,也可以。

我们已经可以使用以下内容来获取报告,但不知道如何使该身份验证也涵盖Office365 Management API服务的运行状况

curl --location --request GET "https://login.microsoftonline.com/{tenantid}/oauth2/v2.0/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data "client_id={clientid}&client_secret={clientsecret}&scope=https://graph.microsoft.com/.default&grant_type=client_credentials"

[将ServiceHealth.Read添加到末尾或单独返回invalid_scope作为错误

当仅将https://manage.office.com/ServiceHealth.Read/.default放在合并范围内时,会给出错误invalid_resource,并给出说明,包括在租户中找不到资源

[尝试获取授权码并将资源设置为ServiceHealth.Read,而将其设置为作用域却给出了授权码时,发生了类似的问题,结果令牌被视为无效。

azure-active-directory office365 microsoft-graph azure-ad-graph-api office365api
1个回答
2
投票

授权码授予流程

我很快通过具有对Office 365管理API的ServiceHealth.Read委派权限的Azure AD应用程序注册进行了尝试。

enter image description here

使用的范围值-https://manage.office.com/ServiceHealth.Read

我能够按照授权码授予流程成功取回访问令牌。我将分享不久后传递的详细请求参数,但这将回答您有关使用哪个范围值的直接问题。

由于我使用了Azure AD V2终结点,因此我实际上并不需要指定资源。在有问题的示例请求中,我看到您也在使用Azure AD V2终结点。

详细步骤

步骤1-获取授权码

对于此步骤,我直接使用浏览器,然后使用我的Azure AD租户中的有效用户登录。

// Line breaks only for clear reading. Remove line breaks and paste in browser URL to test.

https://login.microsoftonline.com/mytenant.onmicrosoft.com/oauth2/v2.0/authorize?
client_id=29a95b.....
&response_type=code
&redirect_uri=https://rohitapp/
&response_mode=query
&scope=https://manage.office.com/ServiceHealth.Read
&state=12345

响应应该类似于

https://rohitapp/?code=
OAQABAAIAAACQN9QBRU....
&state=12345&session_state=f5da06....

步骤2-从令牌端点获取令牌

从上一步获得授权代码。

对于这一步,我使用了POSTMAN。您也可以使用CURL。

POST https://login.microsoftonline.com/mytenant.onmicrosoft.com/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

请求正文

client_id=29a95b....
&scope=https://manage.office.com/ServiceHealth.Read
&code=OAQABAAIAAACQN9QBRU....
&redirect_uri=https://rohitapp/
&grant_type=authorization_code
&client_secret=Aj....

收到的最终令牌,在https://jwt.ms中解码

enter image description here

客户凭证授予流程

使用的范围值-https://manage.office.com/.default

我确实添加了相关的应用许可并表示同意。

为此,我再次使用POSTMAN。您也可以使用CURL。

POST https://login.microsoftonline.com/mytenant.onmicrosoft.com/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

请求正文

client_id=29a95....
&scope=https://manage.office.com/.default
&grant_type=client_credentials
&client_secret=Aj....

收到的最终令牌,在https://jwt.ms中解码

enter image description here

参考scopeClient Credentials Grant值查看此Microsoft文档。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.