nvh95/react-linkedin-login-oauth2

Issue to get the accessToken using the code from this module

versa-dev opened this issue · 1 comments

I got the code from the response data using this module, and tried to get the accessToken using that code, but got the error.
My code and the error like followings;

Linkedin tag

<LinkedIn response_type='code' clientId="7766ia9vsfbyxb" scope='r_emailaddress+r_liteprofile' redirectUri='https://dev.d9jruxpfxzt30.amplifyapp.com/home' onFailure={this.handleFailure} onSuccess={this.handleSuccess} />

request to get the accessToken

  `let config = {
    method: 'post',
    url: `https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code=${data.code}&redirect_uri=https://dev.d9jruxpfxzt30.amplifyapp.com/home&client_id=7766ia9vsfbyxb&client_secret=GKCUsY4o71dYd0iJ`,
    headers: { 
      'Content-Type': 'application/x-www-form-urlencoded',
    }
  };`

#I have tested this request in Postman, following is the error in postman:
{
"error": "invalid_request",
"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"
}

nvh95 commented

@versa-dev You shouldn't place the attributes directly to the url. It depends on languages you are using to make a request. Bellow are some examples:

  • cURL
curl --location --request POST 'https://www.linkedin.com/oauth/v2/accessToken' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'code=your-authorization-code-here'
  • Node.js (with axios)
var axios = require('axios');
var qs = require('qs');
var data = qs.stringify({
  'grant_type': 'authorization_code',
  'code': 'your-authorization-code-here' 
});
var config = {
  method: 'post',
  url: 'https://www.linkedin.com/oauth/v2/accessToken',
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded', 
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});
  • Python
import requests

url = "https://www.linkedin.com/oauth/v2/accessToken"

payload='grant_type=authorization_code&code=your-authorization-code-here'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

The error message Unable to retrieve access token: appid/redirect uri/code verifier does not match authorization code. Or authorization code expired. Or external member binding exists indicates either you made a request already (that makes the authorization code invalid), or the code is just expired (accord to the docs, it has 30 minutes life-span)