Assistance Required: 401 Unauthorized Error with OAuth 2.0 Token for Jira Cloud API

Kürşad ÇEVİK
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!
May 28, 2024

Hello,

I am encountering a persistent issue with accessing the Jira Cloud API using OAuth 2.0 token authentication. Despite following the documentation and ensuring that all required scopes and configurations are set correctly, I am continuously receiving a 401 Unauthorized error. Below are the detailed steps I have taken and the responses received:

 

  • In the Atlassian Developer Console, I created an OAuth 2.0 app and added the required scopes: read:jira-user and read:jira-work.
  • I successfully directed the user to the authorization URL, obtained the authorization code, and exchanged it for an access token.
  • The token includes the correct scopes and appears to be valid.
  • Using the obtained access token, I attempted to access various Jira API endpoints, including /rest/api/3/myself and a specific issue endpoint /rest/api/3/issue/DEMO-1

 


# OAuth2 configuration
client_id = 'client_id'
client_secret = 'client_secret'
authorization_base_url = 'https://auth.atlassian.com/authorize'
token_url = 'https://auth.atlassian.com/oauth/token'
redirect_uri = 'https://demo'

# Initialize OAuth2Session
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope="read:jira-user read:jira-work")

# Direct user to authorization URL
authorization_url, state = oauth.authorization_url(authorization_base_url)


# Fetch the access token
token = oauth.fetch_token(token_url, authorization_response=redirect_response, client_secret=client_secret)


# Define API URL
jira_api_url = 'https://demo.atlassian.net/rest/api/3/issue/demo-1'

# Set headers
headers = {
'Authorization': f'Bearer {token["access_token"]}',
'Accept': 'application/json'
}

# Make API request
response = requests.get(jira_api_url, headers=headers)

 

{
'Date': 'Tue, 28 May 2024 09:06:48 GMT',
'Content-Type': 'application/json;charset=UTF-8',
'Content-Length': '53',
'Server': 'AtlassianEdge',
'Timing-Allow-Origin': '*',
'X-Arequestid': 'af12f852e377c6b484923a965f75ecdd',
'Set-Cookie': 'atlassian.xsrf.token=XXX; Path=/; SameSite=None; Secure',
'Cache-Control': 'no-cache, no-store, no-transform',
'Www-Authenticate': 'OAuth realm="https%3A%2F%2Fdemo.atlassian.net", OAuth realm="https%3A%2F%2Fdemo.atlassian.net"',
'X-Content-Type-Options': 'nosniff',
'X-Xss-Protection': '1; mode=block',
'Atl-Traceid': 'XX',
'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload',
'Report-To': '{"endpoints": [{"url": "https://dz8aopenkvv6s.cloudfront.net"}], "group": "endpoint-1", "include_subdomains": true, "max_age": 600}',
'Nel': '{"failure_fraction": 0.001, "include_subdomains": true, "max_age": 600, "report_to": "endpoint-1"}'
}

 

2 answers

0 votes
Neil
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 4, 2024

Hi @Kürşad ÇEVİK . Were you referencing these docs when trying to implement OAuth2? https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/

If so, check out the section 3. Make calls to the API using the access token - notice that you'll need to use the `cloudId` and place that into the request URL to look something like this:

https://api.atlassian.com/ex/jira/{cloudid}/rest/api/3/issue/DEMO-1
0 votes
Kürşad ÇEVİK
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!
May 28, 2024

I think I opened the topic in the wrong category. I apologize for that.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events