我们已经在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
,而将其设置为作用域却给出了授权码时,发生了类似的问题,结果令牌被视为无效。
我很快通过具有对Office 365管理API的ServiceHealth.Read
委派权限的Azure AD应用程序注册进行了尝试。
使用的范围值-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中解码
使用的范围值-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中解码
参考scope
的Client Credentials Grant值查看此Microsoft文档。