【问题标题】:Unable to get refresh token of https://management.azure.com/ using PowerShell无法使用 PowerShell 获取 https://management.azure.com/ 的刷新令牌
【发布时间】: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


【解决方案1】:

规范规定 客户端凭据 授权类型不得允许发布刷新令牌。所以答案是,您必须使用不同的授权类型来接收带有访问令牌的刷新令牌。

因此,建议您使用auth code flow,它会在您请求令牌时返回刷新令牌给您。

更新:

【讨论】:

  • 那么,这个授权类型应该是什么?
  • 我猜body参数是错误的发出请求。你能建议正确的参数吗? @卡尔赵!
  • @Ashish-BeJovial 首先需要根据文档在浏览器中请求“code”,然后使用“code”请求token。
  • 这段代码是从某个地方生成的还是我可以粘贴任何代码值?
  • 这段代码是唯一的,需要你根据你的请求参数在浏览器中获取,在我提供的文档中有详细的解释。
猜你喜欢
  • 2019-10-26
  • 2013-08-23
  • 2022-12-14
  • 1970-01-01
  • 2021-06-12
  • 2020-08-05
  • 2020-09-25
  • 1970-01-01
  • 2016-07-10
相关资源
最近更新 更多