可以选择通过添加 offline_access 范围来获取 access_token 和 refresh_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
当我尝试使用上面的刷新 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
回应:
要使用刷新 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
或者,您可以使用下面的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
回应:
当我再次生成访问 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
回应:
关于azure - 如何在没有客户端 key 的情况下刷新企业应用程序的 Microsoft Graph API token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75192927/