I am working on a script that uses Jira's REST API to gather all customer information from our project and print it. This is just a small test to pull data from Jira.
The following is what i've cobbled together from resources to do this. Adminemail and API token are filled out properly, along with the website URL. I am utilizing Python to write the script and interact with the REST API.
import requests
import json
import base64
jiraCred = "Basic " + base64.b64encode(b'Adminemail@email.com:APITOKEN').decode("utf-8")
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"X-ExperimentalApi": "opt-in",
"Authorization": jiraCred
}
url = "https://oursite.atlassian.net/rest/servicedeskapi/servicedesk/6/customer"
response = requests.request(
"GET",
url,
headers=headers
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
With all this I still seem to get, " "message": "Client must be authenticated to access this resource." So something must be going wrong with how I'm authenticating the user and API token.
Does anyone have any experience with this and might know what i'm doing wrong?
Hi Patrick,
Your code is looking fine. To use API token you have to use Basic Authentication. This is how your Authorization header should look like:
Basic Base64Encoded(username:apitoken).
So if your user name and tokens are demo:demo it would be
Basic ZGVtbzpkZW1v
You can try out the API in clients like Postman to verify that your headers are correct.
Regards,
Ankit
You do not need to base64 encode the email and token, see example below and see if that works for you.
import json
import requests
from requests.auth import HTTPBasicAuth
email = "youremail@example.com"
token = "APITOKEN"
auth_request = HTTPBasicAuth(email, token)
headers = {
"Content-Type": "application/json",
"X-ExperimentalApi": "opt-in"
}
url = "https://oursite.atlassian.net/rest/servicedeskapi/servicedesk/6/customer"
response = requests.get(url, auth=auth_request, headers=headers)
data = json.loads(response.content)
print(data)
use an IDE to make sure the spaces and indents are correct, although none should be needed since this is basically Globals
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.