# How to get AUTHORIZATION\_CODE with Jira OAuth?

**URL:** <https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285>\
**Category:** Jira Cloud\
**Tags:** oauth2, 3lo, jira-cloud, rest-api\
**Created:** [April 19, 2024, 8:02am UTC](https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285 "2024-04-19T08:02:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SeikyoCho](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@SeikyoCho](https://community.developer.atlassian.com/u/SeikyoCho)\
**Post date:** [April 19, 2024, 8:02am UTC](https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285/1 "2024-04-19T08:02:01Z")

</div>

From the document:  
[https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/](https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/)

This way can get authorization code:

```auto
https://auth.atlassian.com/authorize?
  audience=api.atlassian.com&
  client_id=YOUR_CLIENT_ID&
  scope=REQUESTED_SCOPE_ONE%20REQUESTED_SCOPE_TWO&
  redirect_uri=https://YOUR_APP_CALLBACK_URL&
  state=YOUR_USER_BOUND_VALUE&
  response_type=code&
  prompt=consent

```

I created an app in [develop console](https://developer.atlassian.com/console/) and set a callback url `http://localhost:3000/oauth/callback` in Authorization. When I tried to get the authorization code in terminal:

```auto
AUTH_URL="https://auth.atlassian.com/authorize\?audience\=api.atlassian.com\&client_id\=[MY_CLIENT_ID]\&scope\=read%3Ajira-work\&redirect_uri\=http%3A%2F%2Flocalhost%3A3000%2Foauth%2Fcallback\&state\=6d7493eaf1e6e58c8a0cdc976c41aac428061bb349d3635b77441fa8e68ea21b\&response_type\=code\&prompt\=consent"

```

Then started a callback page local.

```auto
const express = require('express');
const app = express();
const port = 3000;

app.get('/oauth/callback', (req, res) => {
    const code = req.query.code;
    const state = req.query.state;

    res.send(`Received authorization code: ${code}, State: ${state}`);
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});

```

However, when run `curl -X GET "$AUTH_URL"` in terminal, I got this error:

```auto
<!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad Request</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 400 – Bad Request</h1></body></html>

```

Where is wrong? Is it possible to add `http://localhost:3000/oauth/callback` to develop console’s `Callback URL`?

---

<div class="post-metadata">

**Author:** ![ibuchanan](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/ibuchanan/32/35815_2.png) [@ibuchanan](https://community.developer.atlassian.com/u/ibuchanan)\
**Post date:** [April 19, 2024, 11:34am UTC](https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285/2 "2024-04-19T11:34:49Z")

</div>

> [@SeikyoCho](#):
>
> When I tried to get the authorization code in terminal

I wonder if your problem isn’t the the callback URL at all but is how you are calling the `authorize` endpoint “in terminal”. Are you trying to curl it? I hope it’s clear that OAuth 2.0’s authorization code flow requires the browser.

There’s nothing wrong with a `localhost` callback URL. In this previous thread, I shared a CLI tool that does that:

> [@OAuth 2.0 token](https://community.developer.atlassian.com/t/oauth-2-0-token/73805/6):
>
> Now that you’ve linked to the docs, I had a quick look. Jira Spoke does apparently “speak OAuth”. I don’t think you should need to be messing around with OAuth 2 on the CLI. It certainly should not be needed by Jira Spoke. That said, maybe you’ll find some diagnostic value making sure that your app settings are valid, so I’ll share some tools here. Install [oauth2c](https://github.com/cloudentity/oauth2c). Install [jq](https://jqlang.github.io/jq/). Install [xh](https://github.com/ducaale/xh). Or, you can quickly switch xh for [httpie](https://httpie.io/), which is CLI compatible. If not, you might be able to change my…

---

<div class="post-metadata">

**Author:** ![SeikyoCho](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@SeikyoCho](https://community.developer.atlassian.com/u/SeikyoCho)\
**Post date:** [April 20, 2024, 8:49am UTC](https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285/3 "2024-04-20T08:49:32Z")

</div>

Thank you @ibuchanan .  
Yes, I tried to curl it `curl -X GET "$AUTH_URL"` and got a 400 static page.

If `localhost` works well for a callback, I think it’s right to run it from terminal via curl.

I tried your script. After setting `ATLASSIAN_APP_3LO_CLIENT_ID` and `ATLASSIAN_APP_3LO_CLIENT_SECRET` and run it by `sh atlassian-oauth2.sh`, got this error:

```auto
# Request authorization

POST https://auth.atlassian.com/oauth/token                                     
Headers:                                                                        
  Content-Type: application/x-www-form-urlencoded                               
Form post:                                                                      
  grant_type: refresh_token                                                     
  refresh_token: --silent                                                       
  client_id: ATLASSIAN_APP_3LO_CLIENT_ID                                   
  client_secret: ATLASSIAN_APP_3LO_CLIENT_SECRET
Response:                                                                       
{                                                                               
  "error": "unauthorized_client",
  "error_description": "refresh_token is invalid"
}

  ERROR 403: unauthorized_client                                              
GET /me HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br
Authorization: Bearer 
Connection: keep-alive
Host: api.atlassian.com
User-Agent: xh/0.22.0

HTTP/2.0 401 Unauthorized
atl-traceid: 600c1a0ed07e4ad6853bdde111b11584
content-length: 37
content-type: application/json
date: Sat, 20 Apr 2024 07:52:03 GMT
nel: {"failure_fraction": 0.001, "include_subdomains": true, "max_age": 600, "report_to": "endpoint-1"}
report-to: {"endpoints": [{"url": "https://dz8aopenkvv6s.cloudfront.net"}], "group": "endpoint-1", "include_subdomains": true, "max_age": 600}
server: AtlassianEdge
strict-transport-security: max-age=63072000; preload
x-content-type-options: nosniff
x-failure-category: FAILURE_CLIENT_AUTH_MISMATCH
x-frame-options: SameOrigin
x-trace-id: 600c1a0ed07e4ad6853bdde111b11584
x-xss-protection: 1; mode=block

{
    "code": 401,
    "message": "Unauthorized"
}

```

This way it seems didn’t set a `redirect_uri`. Are the `client_id` and `client_secret` enough to use?  
Why the [document](https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/#1--direct-the-user-to-the-authorization-url-to-get-an-authorization-code) shows only `client_id`?

---

<div class="post-metadata">

**Author:** ![ibuchanan](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/ibuchanan/32/35815_2.png) [@ibuchanan](https://community.developer.atlassian.com/u/ibuchanan)\
**Post date:** [April 20, 2024, 10:50am UTC](https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285/4 "2024-04-20T10:50:09Z")

</div>

> [@SeikyoCho](#):
>
> Yes, I tried to curl it `curl -X GET "$AUTH_URL"` and got a 400 static page.
> 
> If `localhost` works well for a callback, I think it’s right to run it from terminal via curl.

No. The `authorize` URL is not a REST API. It will never work through curl.

---

<div class="post-metadata">

**Author:** ![SeikyoCho](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@SeikyoCho](https://community.developer.atlassian.com/u/SeikyoCho)\
**Post date:** [April 21, 2024, 2:32pm UTC](https://community.developer.atlassian.com/t/how-to-get-authorization-code-with-jira-oauth/79285/5 "2024-04-21T14:32:26Z")

</div>

Hi @ibuchanan  
Thank you.  
I use `simple-oauth2` library with a callback url and access from browser, it works.
