0

I am trying to make a simple api call using Python to Azure DevOps to get list of users. URL is providing results via browser but getting error while scripting as below. I need to further proceed with the json response. Can some one help please? Python version: 3.8.3

Script

import requests
import json
response = requests.get('https://vssps.dev.azure.com/siva*****/_apis/graph/users')
print(response)
print(response.json())

**Output:**
<Response [203]>
Traceback (most recent call last):
  File "c:/Users/HP/OneDrive/Documents/github/terraform/azure-aks-kubernetes-masterclass/25-Azure-DevOps-Terraform-Azure-AKS/test.py", line 5, in <module>
    print(response.json())
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\models.py", line 900, in json      
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 3 column 1 (char 4)
2
  • This seems like an authentication issue. You haven't authenticated so the URL will redirect to a login page. That is just HTML. What does line 4 print? Commented Feb 6, 2021 at 9:04
  • @JarroVGIT Thank you. as you pointed out, authentication is the issue. It is solved now.. Commented Feb 7, 2021 at 4:09

1 Answer 1

2

The 203 error is Authentication error when you running the Rest API.

You could use PAT(Personal Access Token) as the Authentication method.

You could grant Graph Read scope to the PAT.

enter image description here

Here is the Python example:

import requests
import base64

pat = 'PAT'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}

response = requests.get(
    url="https://vssps.dev.azure.com/{Organization}/_apis/graph/users?api-version=6.0-preview.1", headers=headers)
print(response)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Kevin. I have accepted the given answer

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.