2

How can I authenticate to Azure DevOps REST API in a python script? I found that there are 2 methods :

  • Using personal access token (PAT)
  • Using OAuth 2.0

I am using the second method. Followed the steps in this documentation: https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops

I wrote this function to autherize to azure DevOps using OAuth 2.0:

def get_authenticated():

    client_id = < my client ID as a string >
    state = "user1"
    scope = "vso.graph_manage%20vso.identity_manage%20vso.profile_write%20vso.project_manage%20vso.tokenadministration%20vso.tokens"
    callback_URL = < Callback URL to my azure devops account >

    # Azure DevOps Services authorization endpoint
    Auth_URL = "https://app.vssps.visualstudio.com/oauth2/authorize?client_id=" + client_id + "&response_type=Assertion&state=" + state + "&scope=" + scope + "&redirect_uri=" + callback_URL
    headers = {'Accept': 'application/json;api-version=1.0'}

    print(Auth_URL)

    response = requests.get(Auth_URL,headers = headers)
    print(response)
    print(response.status_code)
    print(response.headers['content-type'])

    response.raise_for_status() 

But when calling this function, output I am getting is:

<Response [203]>
203
text/html; charset=utf-8

The auth URL is correct because when I tried to access the same URL in a browser it successfully redirects to a form to enter azure user credentials.

The expected behavior of the script is, when the auth_url is requested, Azure DevOps Services should ask the user to authorize. I think that should be done by prompting for username&password in terminal/via a browser.

I am totally new to python scripting and REST APIs. Can someone help me by pointing out the faults in my code or pointing to some samples?

1
  • Hi, did you get a solution to this question? Commented Aug 11, 2021 at 16:06

1 Answer 1

0

The http error 203 indicates that the returned metainformation is not a definitive set of the object from a server with a copy of the object, but is from a private overlaid web. In your code,you added headers = {'Accept': 'application/json;api-version=1.0'}, but in fact the content type should be application/x-www-form-urlencoded.

You can use some OAuth2 library for python to authenticate to Azure DevOps REST API, such as OAuthLib. It includes sevelral samples.

Also, you can refer to following topic, hope it is helpful for you.

Tutorial for using requests_oauth2

Sign up to request clarification or add additional context in comments.

8 Comments

I tried with the header you suggested. But the same error occurred.
can I pass personal access token (PAT) directly via header to access the azure-devops REST API?
@AnjanaDyna, you can not pass personal access token (PAT) directly via header to access the azure-devops REST API. If you want to use PAT authentication, you can refer to this link.
I tried it. The authentication was successful with that method, for sure. But i was not able to use the python client api to create a new user in azure-devops. I posted it as a question here :stackoverflow.com/questions/56591886/… . Could you plaese help me?
You may try to remove the headers = {'Accept': 'application/json;api-version=1.0'}, using default instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.