azure - 如何在没有客户端 key 的情况下刷新企业应用程序的 Microsoft Graph API token ?

标签 azure microsoft-graph-api microsoft365

可以选择通过添加 offline_access 范围来获取 access_tokenrefresh_token。我们还可以使用语法发送请求以获取始终

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

grant_type=refresh_token&
refresh_token=[REFRESH TOKEN]&
client_id=[APPLICATION ID]&
client_secret=[PASSWORD]&
scope=[SCOPE]&
redirect_uri=[REDIRECT URI]

但企业应用程序不允许创建客户端 key 。有没有其他方法可以让我的访问 token 在使用或不使用 PowerShell 的情况下保持事件状态?

我试图保持连接,但无法做到。

最佳答案

Without including client secret, you cannot refresh the access tokens

我尝试在我的环境中重现相同的结果并得到以下结果:

我通过在如下范围内提供 offline_access 获得刷新 token :

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id:appID
client_secret:secret
grant_type:authorization_code
scope:offline_access user.read
code:code
redirect_uri:https://jwt.ms

enter image description here

当我尝试使用上面的刷新 token 获取访问 token 而不提供客户端 secret 时,我得到了错误,如下所示:

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

redirect_uri: https://jwt.ms
client_id:appID
grant_type:refresh_token
refresh_token: <refresh token>
scope: https://graph.microsoft.com/.default

回应:

enter image description here

要使用刷新 token 获取访问 token ,您必须包含client_secret,如下所示:

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

redirect_uri:https://jwt.ms
client_id:appID
client_secret: secret
grant_type:refresh_token
refresh_token: <refresh token>
scope: https://graph.microsoft.com/.default

enter image description here

或者,您可以使用下面的PowerShell 脚本来创建 token 生命周期策略,使访问 token 保持有效24 小时

$policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"23:59:59"}}') -DisplayName "WebPolicyScenario" -IsOrganizationDefault $true -Type "TokenLifetimePolicy"
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq '<service principal display name>'"
Add-AzureADServicePrincipalPolicy -Id $sp.ObjectId -RefObjectId $policy.Id

回应:

enter image description here

当我再次生成访问 token 时, token 生命周期增加,如下所示:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id:appID
client_secret:secret
grant_type:authorization_code
scope:offline_access user.read
code:code
redirect_uri:https://jwt.ms

回应:

enter image description here

引用:
Configurable token lifetimes - Microsoft

关于azure - 如何在没有客户端 key 的情况下刷新企业应用程序的 Microsoft Graph API token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75192927/

相关文章:

azure - 无法通过服务构造连接到现有的azure数据库

azure - 如何为 Multi-Tenancy Azure应用程序创建应用程序后台作业

php - 创建独立的 MS Graph 登录门户

azure - 通过graph api或graph sdk为整个B2C实例创建用户自定义属性

azure - 是否可以在 PowerBI 中查看整个 M365 租户的 Exchange 数据?

sharepoint-online - 嵌入式 SharePoint Online Excel 文件不显示水平滚动条

vba - Excel 365 公式 - 如何永久保存 LAMBDA 函数(对于所有现有文件和新文件)?

azure - 如何从 Azure API 管理登录到 App Insights 'send-request'

azure - 为什么 NuGet 恢复任务必须与构建任务位于同一作业中?

microsoft-graph-api - 使用 MS Graph API 订阅/导入 iCal URL