Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Get Authorization Bearer Token API

I'm just wondering in this article https://learn.microsoft.com/en-us/rest/api/resources/tenants/list enter image description here there's a "try it" button once you click it, it will list all your tenant or directory. enter image description here then once you select any of the directory it will give you a bearer token. enter image description here.

The question is, is there's a way to get a bearer token thru API? Or get a bearer token that depends on the selected tenant? Thanks!

By the return token of that site, im passing it thru this api https://app.vssps.visualstudio.com/_apis/accounts to get all my organization base on the selected tenant.

like image 842
user2530833 Avatar asked Sep 13 '25 04:09

user2530833


1 Answers

If you want to work with the command in PowerShell, the Get-AzAccessToken cmdlet can fetch a token for you.

I tested the following script in PowerShell on Azure Cloud Shell:

$token = (Get-AzAccessToken -ResourceUrl 'https://management.azure.com').Token
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization","Bearer $token")

$url = "https://management.azure.com/tenants?api-version=2020-01-01"

# Send the request
Invoke-RestMethod $url -Method 'Get' -Headers $headers
like image 79
fzieger Avatar answered Sep 14 '25 20:09

fzieger