Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

REST API Authentication

Patrick O'Hara
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 24, 2020

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=Trueindent=4separators=(","": ")))  

 

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?

2 answers

1 vote
Ankit
Atlassian Partner
August 24, 2020

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

0 votes
Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2020

Hi @Patrick O_Hara 

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events