'GET requests to Zendesk API fail using Python 3.x with the HTTP code 401 Unauthorised

I'm simply testing out the capabilities of Zendesk API with the intention of creating a ticket-based Slack application but I cannot seem to get past the simple act of authenticating myself.

I am using a token for authentication and the requests library as instructed on Zendesk's help desk but I still seem to be getting the same

Status: 401 Problem with the request. Exiting.

error message.

I currently have two versions of a Python3 code that have the same output each time.

Passing the Authorization header

import requests
import base64

# Set the request parameters
url = 'https://domain.zendesk.com/api/v2/tickets.json'
user = 'email'
pwd = 'token'

# Create the basic auth header
auth = user + '/token:' + pwd
auth = auth.encode("utf-8")
auth = base64.b64encode(auth)
auth = auth.decode("utf-8")
headers = {'Authorization': 'Basic ' + auth}

# Do the HTTP get request
response = requests.get(url, headers=headers)

# Check for HTTP codes other than 200
if response.status_code != 200:
    print('Status:', response.status_code, 'Problem with the request. Exiting.')
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data.text)

Using auth tuple in the requests.get function

import requests

# Set the request parameters
url = 'https://domain.zendesk.com/api/v2/tickets.json'
user = 'email' + '/token'
pwd = 'token'

# Do the HTTP get request
response = requests.get(url, auth=(user, pwd))

# Check for HTTP codes other than 200
if response.status_code != 200:
    print('Status:', response.status_code, 'Problem with the request. Exiting.')
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data.text)

I have also tried cURL which seems to be working without any hiccups. My guess is that it has something to do with how requests library is coded, however, I could not seem to find the answer anywhere.

Any help would be much appreciated.



Solution 1:[1]

According to the docs you need to enable password and email as auth

https://developer.zendesk.com/documentation/ticketing/getting-started/zendesk-api-quick-start/#preparation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ankush Rathour