azure-active-directory - 设置什么范围和/或资源以获取适用于 office 365 api 的 token ?

标签 azure-active-directory office365 microsoft-graph-api azure-ad-graph-api office365api

我们已在委托(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 的委派权限。

enter image description here

使用的范围值 - 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 中解码

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....

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

enter image description here

查看有关 scope 的 Microsoft 文档Client Credentials Grant 的值.

enter image description here

关于azure-active-directory - 设置什么范围和/或资源以获取适用于 office 365 api 的 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58402344/

相关文章:

outlook - Zapier:修改 webhook 监听器 HTTP 响应?

ms-office - O365 同意流程中的身份验证和权限错误

azure - 使用电子邮件地址作为用户名邀请外部用户加入 Azure B2C

microsoft-graph-api - 使用 microsoft graph 获取新电子邮件

c# - 澄清如何使用 Microsoft.Graph 客户端更新(修补)对象

azure - myapps.microsoft.com Azure AD 中外部用户可见的所有用户

c# - 使用图形 API 以编程方式在 Azure 事件目录中注册应用程序

azure - 关于 Azure AD Connect 目录配置

excel - Office 365 中 Excel 中 Power Pivot 的计算列中正确的 DAX GROUPBY 语法是什么

javascript - 从 Microsoft Graph Api 访问组时出现问题