Forums

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

Unable to connect to JIRA from python using REST API - You are not authenticated. Authentication req

madhan baskar March 13, 2019

Unable to connect to JIRA from python using REST API

Error Message - You are not authenticated. Authentication required to perform this operation

Python code :

import json
import requests
import urllib3
urllib3.disable_warnings()
url = 'https://domainname.atlassian.net/rest/api/3/project'
data = {"key": "sangi","description": "Project_Monday","name": "learn_jira_sangi","projectTypeKey": "software","leadAccountId" : "5c7cb96ee6##########"}
data_json = json.dumps(data)
headers = {'Bearer': 'CfZP25G############', 'Content-Type':'application/json','Accept':'application/json'}
response = requests.post(url, data=data_json, headers=headers , verify=False)
resp=response.json()
print(resp)

 

1 answer

0 votes
Warren
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.
March 13, 2019

Hi @madhan baskar 

I don't use Python, I use C#, and I still use base64 encoded username and password, but ...

The format I use seems different, I have

newRequest.Headers.Add("Authorization", "Basic " + base64Credentials)

and for the Tempo API, where I do use a bearer token, the format is

newRequest.Headers.Add("Authorization", "Bearer " + "xxxxxxxxxx")

So the point I'm making is that both mine start with the word Authorization which you don't have - try to add it in.

P.S. You should remove (mask) your bearer token and company URL - other people could get use those details

madhan baskar March 13, 2019

I have tried in this way - 

headers = {'Authorization': 'Bearer = CfZP25GHMajE356', 'Content-Type':'application/json','Accept':'application/json'}

Also in this way - 

headers = {'Authorization': 'Bearer' + 'CfZP25GHajE356', 'Content-Type':'application/json','Accept':'application/json'}

 Also in this way - 

headers = {'Authorization', 'Bearer' + 'CfZP25GH9E356', 'Content-Type':'application/json','Accept':'application/json'}

Nothing worked!

Getting same error - {'errorMessages': ['You are not authenticated. Authentication required to perform this operation.'], 'errors': {}}

Warren
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.
March 13, 2019

Hi @madhan baskar 

You may still want to try base64 encoding your username and password - this StackOverflow answer shows a Python implementation

madhan baskar March 13, 2019

The link provided here is also not giving the solution.. Can you help me out with any other option?

Stuck in this for more than a day!:(

Warren
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.
March 13, 2019

@madhan baskar 

Possibly post your question on the Developer Community, it's a more technical group and someone there may use Python

Warren
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.
March 14, 2019

@madhan baskar 

I've been looking into the use of an API token for Jira and it's different to what Tempo use, so what I was asking you earlier to try is nonsense.

This page describes how to use the Jira token and they say you can just replace your password with the token. I've tried that, still using the base64 encoding, and it works. So the format would be 

newRequest.Headers.Add("Authorization", "Basic " + base64Credentials)

where the base64Credentials  is created using my username (which is e-mail address) and the generated token.

For completeness, in C# the encoded credentials routine looks like this

string mergedCredentials = string.Format("{0}:{1}", m_Username, m_token);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);

Hopefully you already have a routine or are able to convert this code to Python

Suggest an answer

Log in or Sign up to answer