Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Python get returning 401


#1

Hi,

I am trying to use python 3.5.2 to get requests to call data from the testrail api.

I am using the current code:

import requests
from requests import HTTPBasicAuth

url = 'https://validtestrailurl/index.php?/api/v2/get_users
user = ‘valid user name’ (Also tried email address)
password = 'valid password’
headers = {‘Content-Type’: ‘application/json’}
r = requests.get(url, auth=(‘user’, ‘password’), headers=headers)
print(“response status code:”, r.status_code)

This is returning as response status code: 401

Why is this not working? Am I incorrectly authenticating the the request ?

Using the testrail python binding works but I want to understand more about how and why it works.

Thanks,


#2

When using print(r.text) it produces the following error: “error”:“External auth: Bind: Invalid credentials”


#3

Hi Sinclair,

Thanks for your posting. The 401 is an authentication issue (incorrect username and/or password) and it seems your TestRail is configured to use LDAP/Active Directory integration. Can you try to add an API key on your My Settings page instead of your username and password? You would need to use your email address and an API key (as the password) in this case. I can also recommend using our official API bindings as they already take care of the authentication, JSON handling etc.:

http://docs.gurock.com/testrail-api2/bindings-python

Cheers,
Tobias