The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

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

Comments for this post are closed

Community moderators have prevented the ability to post new 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 - ELFAPP
Atlassian Partner
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

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events