azure command line ( az-cli ) expires after couple days?

Faraz 1 Reputation point
2021-12-20T15:13:04.353+00:00

I want to use azure command line to automate starting and stopping VMs. The problem is my session expires after only a couple days. It gives the message:

AADSTS50173: The provided grant has expired due to it being revoked, a fresh auth token is needed. The user might have changed or reset their password. The grant was issued on '2021-12-17T18:24:41.0656418Z' and the TokensValidFrom date (before which tokens are not valid) for this user is '2021-12-20T08:24:05.0000000Z'.

Obviously having to manually log in every couple days defeats the whole purpose of my automation idea..

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,078 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,346 Reputation points
    2021-12-20T16:05:54.097+00:00

    You need to use a refresh token. OAuth gives you an initial token to use to authenticate and a refresh token assuming you're using a standard client credential grant type (which services to services tend to use). When the original auth token expires you can contact the OAuth server again and pass it the refresh token to get an updated token that you can then continue to use until it expires. In my experience OAuth servers can return refresh tokens indefinitely so you only need to "log in" once but it depends on the implementation. As part of the original auth request it also tells you how long the token is good for.

    Alternatively you could also probably just call the az login (or whatever that command is) when the token expires to log in again anyway.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.