Search code examples
azureazure-devopsazure-active-directory

203 Non-Authoritative Information for listing the pat tokens from Azure DevOps using RestAPI with bearer token


I am trying to list out the PAT tokens in our Azure DevOps Organization using Bearer Token, however I am getting below error shown in the screenshot.

enter image description here

Generated bearer token using client credentials flow:

POST https://login.microsoftonline.com/<tenantID>/oauth2/token
grant_type:client_credentials
client_id: <appID>
client_secret: <secret>
resource: https://management.azure.com/

I am following https://learn.microsoft.com/en-us/rest/api/azure/devops/tokens/?view=azure-devops-rest-7.0&tabs=powershell document.


Solution

  • I tried to reproduce the same in my environment and got below results:

    I generated one access token using client credentials flow via Postman with below parameters:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/token
    grant_type:client_credentials
    client_id: <appID>
    client_secret: <secret>
    resource: https://management.azure.com/
    

    Response:

    enter image description here

    When I used this token to list PAT tokens, I got same error as you like below:

    GET https://vssps.dev.azure.com/sridevops14/_apis/Tokens/Pats?api-version=6.1-preview
    

    enter image description here

    To resolve the error, you need to generate access token with delegated flows by changing scope to 499b84ac-1321-427f-aa17-267ca6975798/.default.

    In my Azure AD application, I added Delegated Azure DevOps API permission and granted admin consent to it like below:

    enter image description here

    You can make use of below authorization request to get code value:

    https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/authorize
    ?client_id=<appID>
    &response_type=code
    &redirect_uri=https://jwt.ms
    &response_mode=query
    &scope= 499b84ac-1321-427f-aa17-267ca6975798/.default
    &state=12345
    

    When I ran above request in browser, I got code value in address bar after signing in successfully like below:

    enter image description here

    Now, I generated access token using authorization code flow by changing scope to 499b84ac-1321-427f-aa17-267ca6975798/.default with below parameters like this:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    grant_type:authorization_code
    client_id: <appID>
    client_secret: <secret>
    scope:499b84ac-1321-427f-aa17-267ca6975798/.default
    code: <paste_code_from_above_request>
    redirect_uri: https://jwt.ms
    

    Response:

    enter image description here

    When I used above access token to list PAT tokens, I got the response successfully like below:

    GET https://vssps.dev.azure.com/sridevops14/_apis/Tokens/Pats?api-version=6.1-preview
    

    Response:

    enter image description here