Hi - I'm trying to access JIRA Server REST API using Python. We currently on Jira 9.2.0 Data center version in dev environment and Jira Server 9.2.0 in Production environment and running code aganist prod or dev produces same errors. Below are the steps I followed to access the API:
1. I have admin access to Jira and created a Personal Access Token under my user profile.
2. I'm using my email address and PAT as the credentials for the BasicAuth
3. I have tried using base64 encoding authentication headers as well
4. URL I'm trying to access is https://jira-dev.mycompany.com/rest/api/2/application-properties (In fact I have tried api/2/project and many other endpoints)
5. When I execute the end point call I receive a 200 status but fails on JSON parsing
6. Printing response.text shows plenty of html code which I believe is actually authentication failing
7. I have tried passing Basic64 encoded credentials in headers as well but still the same error.
8. Blaming it on Python, I have tried from PowerShell and I still get the same error (this is a working piece of code that we are currently using aganist Jira cloud)
Below is my code snippet I have redacted the actual credentials but leaving the format in place so you get the gist.
Python using BasicAuth
import requests
from requests.auth import HTTPBasicAuth
import json
url = "https://jira-dev.company.com/rest//api/3/application-properties"
auth = HTTPBasicAuth("fist.last@company.com", "mytoken")
headers = {
'Content-Type': 'application/json'
}
response = requests.get(
url,
headers=headers,
auth=auth
)
print(response.status_code)
#print(response.text)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
Python using encoded credentials
import requests
import base64
text = 'first.last@mycompany.com:mytoken'
text_bytes = text.encode('Ascii')
base64_bytes = base64.b64encode(text_bytes)
url = "https://jira-dev.mycompany.com/rest//api/2/application-properties"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic %s' % base64_bytes
}
response = requests.get(
url,
headers=headers
)
print(response.status_code)
print(response.text)
PowerShell using Basic64 encoding
-----------------------------------------------------------
$Uri= "https://jira-dev.mycompany.com/rest/api/2/application-properties"
$AdminUser = "first.last"
$Token = "mytoken"
# The Header is created with the given information.
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $AdminUser, $Token)))
$Header = @{
Authorization = ("Basic {0}" -f $base64AuthInfo)
}
# Splat the parameters in a hashtable for readability
$UsersParameters = @{
Method = "GET"
Headers = $Header
Uri = $Uri
}
$response = Invoke-RestMethod @UsersParameters
echo $response
---------------------------------------------------------------------
All three variations produced same result:
Response code of 200, failing to parse JSON elements in response and the response coming back in html.
Any help will be greatly appreciated.
Thanks,
Smitha
Hello @Smitha Pasham
You should try using your username and password and not the PAT. Check also if you do not have a captcha on jira login page.
Regards
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.