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: 
  • Community
  • Q&A
  • Jira
  • Questions
  • express js code results invalid JSON output when I provide authorization code to get access token

express js code results invalid JSON output when I provide authorization code to get access token

Natnael Tamrat
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!
July 7, 2021

const express=require('express')

const fetch=require('node-fetch')

var cookieParser = require('cookie-parser');

const app =express()app.use(express.text());

app.use(cookieParser());

const clientId = "Z7SnfR0hygWOK4PQ42L7F8Mfb0l6QyJs"

const clientSecret = "pz2ktUf6eQ5tAW30YGikbxEsvGumpoZY15kh0RU1TQE2JAywivNEMPtiMWB34B49"

const YOUR_USER_BOUND_VALUE="3sdssdw453hh3"

app.get('/login',(req,res)=>{const url = `http://auth.atlassian.com/authorize?audience=api.atlassian.com&client_id=Z7SnfR0hygWOK4PQ42L7F8Mfb0l6QyJs&scope=read%3Ajira-user&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Flogin%2Fjira%2Fcallback&state=${YOUR_USER_BOUND_VALUE}&response_type=code&prompt=consent`res.redirect(url);//res.send("hello");

https://natnael-tamrat.atlassian.net/plugins/servlet/oauth/access-token})

async function getAccessToken(code){    

const resawait fetch(``,{     

   method:"POST",  

  headers: {        "Content-Type":"application/json"    },  

      body :{"grant_type":"authorization_code" ,"client_id": `${clientId}`,"client_secret":`${clientSecret}`,"code":`${code}`,"redirect_uri": "https://localhost:5000/login/jira/callback"}    })        const data =await res.text();       

 console.log(data);

    //const params=new URLSearchParams(data)    

return data//params.get('oauth_token')}

app.get('/login/jira/callback',async (req,res)=>{    const code =req.query.code   

     const token =await getAccessToken(code)  

  res.send(token);    //res.send(token);})

const PORTprocess.env.PORT || 5000app.listen(PORT ,()=>{    console.log("listening on port 5000");})

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Daniel Eads
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 8, 2021

Hi @Natnael Tamrat !

Firstly, I see that Dario assisted you with your question last week about using Basic Auth , and that he mentioned the dedicated Developer Community might be good to check out for development related questions. Since the topics are more focused, it's easier for other developers to watch the questions coming in and provide guidance.

For your specific issue - I'm not a javascript developer, but it looks to me like you may be manually trying to generate a JSON Web Token. Our documentation has information about using JWT with Jira that would be a good place to start.

More specifically though - we do provide a javascript library for interacting with Atlassian-generated JWTs that I highly recommend integrating into your app. This should simplify your code, as well as guarantee the tokens are being generated in the way Jira expects.

Hope that helps give a solid pointer!
Daniel | Atlassian Community

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events