API Call failed with token expired From Powershell Script

Viewed 176

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"
0 Answers
Related