我们已在委托(delegate)和应用程序中获得管理员同意 ServiceHealth.Read
我们在 Azure AD 中的客户端应用程序的 Office 365 管理 API 中的权限。
如果我们想调用 office365 管理 api,我们无法确定在 token 获取过程中需要什么范围和/或资源。
无论是client_credentials
直接代币获取的授予方式
或 authorization code
然后是登录用户方法的 token
如果它用于 client_credentials
会更好。 grant 方法,但如果它必须通过 auth 代码,那也没关系。
我们已经可以使用以下内容来获取我们的报告,但不知道如何允许该身份验证也涵盖 Office365 管理 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
时出现类似问题并且虽然将其设置为范围而不是提供授权代码,但生成的 token 被认为是无效的。
最佳答案
授权码授予流程
我使用具有 的 Azure AD 应用程序注册快速尝试了这一点。 ServiceHealth.Read
Office 365 管理 API 的委派权限。
使用的范围值 - https://manage.office.com/ServiceHealth.Read
我能够按照授权代码授予流程成功取回访问 token 。我将分享稍后传递的详细请求参数,但这应该回答您关于使用什么范围值的直接问题。
由于我使用了 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 步 - 从 token 端点获取 token
从上一步获取授权码。
对于这一步,我使用了 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....
收到的最终 token ,在 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....
收到的最终 token ,在 https://jwt.ms 中解码
查看有关
scope
的 Microsoft 文档Client Credentials Grant 的值.关于azure-active-directory - 设置什么范围和/或资源以获取适用于 office 365 api 的 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58402344/