So, I am trying to use the Azure DevOps REST API with an application registration in Entra to authenticate. Azure DevOps is connected to my tenant, and the application is added as an User that’s been given permissions. I’d like for my personal account to be able to authenticate impersonating as the Application, but i would also like to move this into perhaps an Azure Function or whatever down the road.
What’s ‘this’? ‘This’ is going to be either my Powershell that is going to create and configure new projects. Perhaps even add users to specific projects so I won’t have to. Anything the Azure DevOps REST API supports which I am required to do manually on a daily/weekly base.
I did read the docs about the OAuth2 flow, but none of that makes any sense to me. The steps aren’t clear enough. Callback URLs, Redirect URIs?
And then still, it wouldn’t be enough for me to know how to use that flow from Powershell.
I also checked in the OAuth2 configuration of my organization on Azure DevOps, but it requires me to choose Bitbucket Cloud, Github or Github Enterprise? I’m not sure why it could only be those three platforms for OAuth2, but my guess is this is not the config I’m looking for.
If anyone could enlighten me to a higher level, and help me out, that would be very appreciated.
3
You can use the following PowerShell script to get the token of the application
$tenantId = "your-tenant-id"
$clientId = "your-client-id"
$clientSecret = "your-client-secret"
$resource = "499b84ac-1321-427f-aa17-267ca6975798" #(the Azure DevOps resource's UUID is 499b84ac-1321-427f-aa17-267ca6975798)
$body = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
resource = $resource
}
# Get the token
$response = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -Body $body
$token = $response.access_token
# Display the token
$token
Then you can call an Azure DevOps API by passing it in the headers as a Bearer
token. For example: Authorization = "Bearer $token"
.
You can also refer this Q&A to get the token with Azure CLI command az account get-access-token.
2