1

Obtain Azure DevOps Personal Access Token programmatically

 2 years ago
source link: https://www.michev.info/Blog/Post/3700/obtain-azure-devops-personal-access-token-programmatically
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Obtain Azure DevOps Personal Access Token programmatically

A colleague approached me with a question about automating the creation of Personal Access Token (PAT) for Azure DevOps. As usual, I tried taking the easy route of doing a quick search online, however all I could find were some convoluted/incomplete examples, and lots and lots of misunderstandingс. Which comes as a surprise, as the task is actually quite simple, once you figure out what it entails. So here’s how you can obtain PAT via few simple API calls (all done via PowerShell).

First, you need to have an Azure AD application, and have the user_impersonation scope for Azure DevOps added to it. In other words, go to the Azure AD blade, create a new app registration or use an existing one. Go to API permissions > Add a permission > select Azure DevOps > select user_impersonation under Delegate permissions > confirm. Not much you can do wrong here, as this is the only permissions you can select for said API anyway. Depending on which users you plan to generate PATs for, you might as well consider granting consent for the entire directory, but that’s optional.

Once permissions have been added/consent granted, you need to obtain an access token. Use the exact same methods you’d use for any other Azure AD integrated application. The scope is the only thing you need to modify, and for it, use the value of “499b84ac-1321-427f-aa17-267ca6975798/.default“. This basically translates to “all scopes for Azure AD DevOps API”. Then, request the token.

Once you have a valid token, you can issue queries against the Azure AD DevOps API, which is again very similar to what you are used to with other Azure AD integrated apps and the Graph API. For example, the query below will return all currently configured PAT tokens for the user:

$uri = "https://vssps.dev.azure.com/vasil0445/_apis/tokens/pats?api-version=7.1-preview.1"
$res = Invoke-WebRequest -Method GET -Uri $uri -Debug -Verbose -Headers $authHeader1
($res.Content | ConvertFrom-Json).PatTokens
displayName : new
validTo : 2022-03-13T15:40:35.1266667Z
scope : vso.work
targetAccounts : {0ced1f28-dfb7-4073-90a7-5f000c7c1550}
validFrom : 2022-02-11T15:40:49.5133333Z
authorizationId : 4721a58e-73b2-41a6-8225-50c022e6fd7d
token :

The one thing that you need to modify in the above request is the organization name. If you don’t want to hardcode it, here’s how to obtain a list of ADO orgs for the current user. First, run a GET request against the /profile/profiles/me endpoint to get the GUID of the user, then pass it in a GET request against the /accounts endpoint and fetch the AccountName value(s) out of the reply:

#Get userId from /profiles
$uri = "https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=7.1-preview.1"
$res = Invoke-WebRequest -Method GET -Uri $uri -Debug -Verbose -Headers $authHeader1
$memberid = ($res.Content | ConvertFrom-Json).Id
#Call /accounts to get the org(s)
$uri = "https://app.vssps.visualstudio.com/_apis/accounts?memberId=$memberId?api-version=7.1-preview.1"
$res = Invoke-WebRequest -Method GET -Uri $uri -Debug -Verbose -Headers $authHeader1
$org = ($res.Content | ConvertFrom-Json).AccountName

Of course, you can have multiple organizations, so handle things as necessary. Or just hardcode it.

Anyway, enough off topic. Here’s how to generate a new PAT for the current user. In a nutshell, you need to run a POST request against the /tokens/pats endpoint and provide a JSON payload with few properties. Those include the displayName, validity (validTo), permissions (scope) and whether the PAT should be valid for the current org, or all orgs the user has access to (allOrgs). Do make sure to use appropriate values here, especially for the scope parameter, copy/paste the below example at your own peril 🙂

#Create a new PAT
$uri = "https://vssps.dev.azure.com/vasil0445/_apis/tokens/pats?api-version=7.1-preview.1"
$body = @{
displayName = "new_token"
scope = "app_token"
validTo = "2022-12-01T23:46:23.319Z"
allOrgs= "false"
}
$res = Invoke-WebRequest -Method POST -Uri $uri -Debug -Verbose -Headers $authHeader1 –ContentType 'application/json' -Body ($body | ConvertTo-Json)
($res.Content | ConvertFrom-Json).patToken.token
girfpsqnpqeci3vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Of course, make sure to capture the PAT string, as it won’t be shown anywhere else. For the sake of completeness, here’s how to get the details on specific PAT:

#Get a specific PAT
$uri = "https://vssps.dev.azure.com/vasil0445/_apis/tokens/pats?authorizationId=4721a58e-73b2-41a6-8225-50c022e6fd7d&api-version=7.1-preview.1"
$res = Invoke-WebRequest -Method GET -Uri $uri -Debug -Verbose -Headers $authHeader1
($res.Content | ConvertFrom-Json).PatToken
displayName : new
validTo : 2022-03-13T15:40:35.1266667Z
scope : vso.work
targetAccounts : {0ced1f28-dfb7-4073-90a7-5f000c7c1550}
validFrom : 2022-02-11T15:40:49.5133333Z
authorizationId : 4721a58e-73b2-41a6-8225-50c022e6fd7d
token :

Do note that the token value will be empty, so again, make sure to capture it upon creation. For more details on working with PATs, refer to the official documentation.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK