繁体   English   中英

如何在没有企业应用程序客户端密钥的情况下刷新 Microsoft Graph API 的令牌?

[英]How to refresh the token of Microsoft Graph API without Client Secret for an Enterprise App?

有一个选项可以通过添加offline_access scope 来获取access_tokenrefresh_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

回复:

在此处输入图像描述

参考:
可配置的令牌生命周期 - Microsoft

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM