【发布时间】:2020-12-10 17:11:14
【问题描述】:
我正在尝试使用 PowerShell 获取“https://management.azure.com/”资源的访问令牌和刷新令牌,但我得到的是唯一的访问令牌。我也需要一个刷新令牌。我分享我的代码如下。
$clientID = '1xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$secretKey = 'kdfudifkldfliKASDFKkdfjd-ddkjfidysikd'
$tenantID = 'fxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$password = ConvertTo-SecureString -String $secretKey -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($ClientID,$password)
Connect-AzureRmAccount -ServicePrincipal -Credential $credential -Tenant $tenantID
$authUrl = "https://login.windows.net/" + $tenantID + "/oauth2/token/"
$body = @{
"resource" = "https://management.azure.com/";
"grant_type" = "client_credentials";
"client_id" = $ClientID
"client_secret" = $secretKey
}
Write-Output "Getting Authentication-Token ..."
$adlsToken = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body
Write-Output $adlsToken
------------输出---------------
Getting Authentication-Token ...
token_type : Bearer
expires_in : 3599
ext_expires_in : 3599
expires_on : 1597999269
not_before : 1597995369
resource : https://management.azure.com/
access_token : J0uYFoioURT4CdISuUrRrr...
【问题讨论】:
-
在您的情况下,正确的方法是再次运行脚本以获取新的访问令牌,而不是使用刷新令牌来获取新的。
标签: powershell oauth-2.0 azure-active-directory access-token refresh-token