最近搜索


没有最近搜索

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



已于 2022年7月18日 发布

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 deskcrypto 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.


1

1

1 条评论

Hi Reece, at this time Zendesk does not provide any examples/support for API use via Python. Similar to what it sounds like you may already be doing, in cases like this, I would recommend trying the call(s) in cURL and/or Postman to ensure the credentials are working as expected, and work back from there, and/or searching the forums for other community responses - cheers!

0


登录以发表评论。

找不到所需的内容?

新建帖子