Unable to get access token for linkedin using axios

▼魔方 西西 提交于 2021-01-29 18:27:42

问题


I am trying to get access token following the steps described in Linkedin Oauth. I am trying to perform step 2 in the process to get an access token. I am using Axios for the POST request. Here is the code I am using (The client secret and id is not real for security reasons):

    const axios = require('axios');
    const qs = require('qs');
    axios({
        method: 'post',
        url: 'https://www.linkedin.com/oauth/v2/accessToken',   
        data: qs.stringify({
            grant_type: 'authorization_code',
            code: 'AQSow7V6s2F2koWzIsnVKcQGt_cHtsM1F3FHZOrEV0UY1KIFkWiFJpi8dt1NtjuZMOO6-NStoCjTf58awk6GBcH2XQRctt7IBtel4Oeop5yVIBqiedk8qxlIlbkMxlfGg1gCVoupXL6xUc3-jegKYDPSe0rl4mygdpIzGdej2_hhJ827vJcojtvaMXCCGw',
            redirect_uri: 'https%3A%2F%2Fwww.linkedin.com%2Fcompany%2Ftttrrr878',
            client_id: '99blt2z20qlm3d',
            client_secret: 'fGGgdqqcx5t3cRfw'       
        }),
        headers: {
            content-type: 'application/x-www-form-urlencoded;charset=utf-8'
        }
        }).then(result => { 
            console.log(result.data);
        }).catch(error => { 
            console.log(error);
        });

I get the following error when I run the code even after making sure that the code is not expired (before the 30 min expiration time):

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'
    }

However when I do the same thing using curl in the command line as follows, I am able to get the access code:

curl -ik -X POST https://www.linkedin.com/oauth/v2/accessToken \
-d grant_type=authorization_code \
-d code=AQSow7V6s2F2koWzIsnVKcQGt_cHtsM1F3FHZOrEV0UY1KIFkWiFJpi8dt1NtjuZMOO6-NStoCjTf58awk6GBcH2XQRctt7IBtel4Oeop5yVIBqiedk8qxlIlbkMxlfGg1gCVoupXL6xUc3-jegKYDPSe0rl4mygdpIzGdej2_hhJ827vJcojtvaMXCCGw \
-d redirect_uri=https%3A%2F%2Fwww.linkedin.com%2Fcompany%2Ftttrrr878 \
-d client_id=99blt2z20qlm3d \
-d client_secret=fGGgdqqcx5t3cRfw

Any idea why this is happening? Is it somehow related to this issue ?


回答1:


Your code looks correct. The issue is because you are url encoding the redirect_uri parameter, remove that and it should work.

You only need to url encode the redirect_uri parameter when doing the initial request for the access code from the browser.



来源:https://stackoverflow.com/questions/62933774/unable-to-get-access-token-for-linkedin-using-axios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!