Andrea D_
Andrea D_

Reputation: 2141

How to get the access token from Linkedin API with express and axios?

I am studying Linkedin API to use the 'sign-in with Linkedin' feature. I am using axios to get the accessToken but I am getting this error: error: 'invalid_redirect_uri',

data: {
  error: 'invalid_redirect_uri',
  error_description: 'Unable to retrieve access token: appid/redirect uri/code verifier does not match authorization code. Or authorization code expired. Or external member binding exists'
}

The thing is that while on Postman, where the redirect_uri is exactly the same, it works and I get the access token back, on express I am getting the above-mentioned error.

async function getAccessToken(authCode) {
  try {
    const response = await axios.post('https://www.linkedin.com/oauth/v2/accessToken', null, {
      params: {
        grant_type: 'authorization_code',
        code: authCode,
        redirect_uri: 'https%3A%2F%2Fswift-front.netlify.app',
        client_id:'86v3d75uyj2qp4',
        client_secret: itsASecret
      },
    })
    console.log(response);
  } catch (error) {
    console.log(error)
  }
}
getAccessToken(authCode);

I guess my mistake is somehow in the way I pass the params. Cannot figure out what exactly is the error.

Upvotes: 1

Views: 1313

Answers (1)

Francesco Orsi
Francesco Orsi

Reputation: 2099

redirect_uri has to be encoded only when you request the Authorization Code, but when you request the Access Token you use the redirect URLs without encoding it.

Upvotes: 3

Related Questions