[英]How to refresh the token of Microsoft Graph API without Client Secret for an Enterprise App?
有一个选项可以通过添加offline_access scope 来获取access_token和refresh_token 。我们也可以发送请求以获取 always 语法
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]
但企业应用程序不允许创建客户端密码。 有或没有 PowerShell,还有其他方法可以使我的访问令牌保持活动状态吗?
我正在努力保持联系,但无法做到。
如果不包含客户端密码,您将无法刷新访问令牌
我试图在我的环境中重现相同的结果并得到以下结果:
我通过在 scope 中提供offline_access
获得了刷新令牌,如下所示:
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
当我尝试在不提供客户端机密的情况下使用上述刷新令牌获取access token
时,出现如下错误:
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
回复:
要使用刷新令牌获取访问令牌,您必须像下面这样包含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 脚本来创建令牌生命周期策略,该策略可以使访问令牌保持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
回复:
当我再次生成访问令牌时,令牌寿命增加如下:
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
回复:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.