0

I'm running a simple call to Azure DevOps API using Powershell:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "{USER}","{PAT}")))
$url = "https://dev.azure.com/{ORG_NAME}/{PROJECT_NAME}/_apis/distributedtask/variablegroups/{ID}?api-version=5.0-preview.1"
Invoke-RestMethod -Uri $url -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}   

The error is shown after:

Invoke-RestMethod: Response status code does not indicate success: 401 (Unauthorized).

Trying to figure out what's wrong, all is configured according to this and this articles.

The strange is that running a call against API without specifying the project is processed without errors:

$url2 = "https://dev.azure.com/{ORG_NAME}/_apis/projects?api-version=2.0" 
Invoke-RestMethod -Uri $url2 -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

Response:

count value
----- -----
    5 {@{id=xxxxxxx-89f3-46b0-af7e-xxxxxxx; name=Xxxxx; description=F…
Sergey
  • 381
  • 6
  • 24
  • Looks like they are only base64 encoding the username [here](https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#q-how-can-i-use-a-pat-in-my-code) and then putting the PAT token in after the colon. `The resulting string can then be provided as an HTTP header in the following format: Authorization: Basic BASE64USERNAME:PATSTRING`. The C# examples then go on to provide an empty string for the username. Couple of things to try. – Ash May 06 '20 at 21:45
  • Hmm, not sure as I tried first to use only encoded PAT token and there was an error. After switching to full encoding (username:PAT) I was able to authenticate. – Sergey May 07 '20 at 04:26

1 Answers1

1

It seems your PAT is not authorized to access the Variable groups.

You can go to your PAT edit page to check if the PAT was assigned at least the Read permission for Variable groups. See below screenshot.

Grant the proper permission scope for your PAT, and try calling the rest api again.

enter image description here

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43