简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM