[英]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.