'How to get an AzureAD JWT for service authentication on command line

I have some company website providing some information protected by azure AD. This works fine in the browser, but I would like to create some automation on command line.

If I copy the JWT from the browser and use it in the Authorization: Bearer header, I can also access that information perfectly fine.

I was also able to use az login to prove my identity on the command line.

What is the correct way to get a suitable JWT on command line in order to access the information?

I was thinking of something like:

az login
token=$(az ad get-new-jwt-token)
curl -H "Authorization: Bearer $token" myinfosite.company.com

If there is a better/different solution, I am open to that, too.



Solution 1:[1]

You are almost there. If you want to do this via CLI then it would be like below

  1. login

  2. set the subscription

  3. get the access token

     az login        
     az account set  -s "<your-subscription-id>"        
     az account get-access-token
    

After that you would get your Bearer token enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ricky G