API呼叫失败,令牌从PowerShell脚本到期

发布于 2025-02-03 09:06:59 字数 1403 浏览 3 评论 0原文

Invoke-restMethod:{“ error”:{“代码”:“ expiredAuthenticationToken”,message“”“访问令牌expiry utc时间早于UTC时间}

脚本运行超过一个小时(确切地运行它,可用于1.40 mins ), the above error happens 1 out of 2 scenarios.

方案:1个

$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)

$nextLink = "https://management.azure.com/subscriptions/$($subscriptionId)/providers/Microsoft.Consumption/usageDetails?`$expand=meterDetails&`$filter=properties%2FusageStart%20ge%20'$($firstOfMonth)'%20and%20properties%2FusageEnd%20le%20'$($lastOfMonth)'&api-version=2019-10-01"

方案:2

$token  = (Get-AzAccessToken -ResourceUrl "https://management.azure.com" - TenantId "").Token

Invoke-RestMethod : {"error":{"code" : "ExpiredAuthenticationToken", message"" "The access token expiry UTC time is earlier than UTC time"}


$nextLink = "https://management.azure.com/subscriptions/$($subscriptionId)/providers/Microsoft.Consumption/usageDetails?`$expand=meterDetails&`$filter=properties%2FusageStart%20ge%20'$($firstOfMonth)'%20and%20properties%2FusageEnd%20le%20'$($lastOfMonth)'&api-version=2019-10-01"

Invoke-RestMethod : {"error":{"code" : "ExpiredAuthenticationToken", message"" "The access token expiry UTC time is earlier than UTC time"}

Script runs For more than an hour (to be precise it runs For 1.40 mins ), the above error happens 1 out of 2 scenarios.

Scenario: 1

$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)

$nextLink = "https://management.azure.com/subscriptions/$($subscriptionId)/providers/Microsoft.Consumption/usageDetails?`$expand=meterDetails&`$filter=properties%2FusageStart%20ge%20'$($firstOfMonth)'%20and%20properties%2FusageEnd%20le%20'$($lastOfMonth)'&api-version=2019-10-01"

Scenario: 2

$token  = (Get-AzAccessToken -ResourceUrl "https://management.azure.com" - TenantId "").Token

Invoke-RestMethod : {"error":{"code" : "ExpiredAuthenticationToken", message"" "The access token expiry UTC time is earlier than UTC time"}


$nextLink = "https://management.azure.com/subscriptions/$($subscriptionId)/providers/Microsoft.Consumption/usageDetails?`$expand=meterDetails&`$filter=properties%2FusageStart%20ge%20'$($firstOfMonth)'%20and%20properties%2FusageEnd%20le%20'$($lastOfMonth)'&api-version=2019-10-01"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文