Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes
Daniel Eads Atlassian Team Jul 08, 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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
Community showcase
Published in Jira

⏰ Day in the life of a Jira Admin!

Hello Community! We thoroughly enjoyed this just-for-fun conversation in the Jira Admin Group about what it's like to be a Jira Admin. For #JiraJuly, our talented designers created these graphics t...

758 views 2 21
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you