简体   繁体   中英

I can't get the linkedin access token with axios, keep getting status 400

The API says that status code 400 is probably syntax error, but I wasn't able to find it. I already have the authentication code and app credentials, and the url is registered.

I've tried with and without qs.

exports.getAccessToken = (req, res, next) => {
    let payload = req.body;
    let request_body = qs.stringify({
        "grant_type": "authorization_code",
        "code": payload.code,
        "redirect_uri": linkedin.redirect_uri,
        "client_id": linkedin.clientId,
        "client_secret": linkedin.clientSecret
    });
    let config = {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
    };
    axios.post("https://www.linkedin.com/oauth/v2/accessToken", request_body, config).then(
        response => {
            res.json(response.data);
        },
        error => {
            res.json(error);
        }
    );
}

You are using the wrong url to get accesstoken.

  • Step 1

First you need to use following url

curl -X GET \
  'https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=[CLIENT_ID]&redirect_uri=https://getpostman.com/oauth2/callback&state=CA&scope=r_emailaddress%20r_ads%20w_organization_social%20rw_ads%20r_basicprofile%20r_liteprofile%20r_ads_reporting%20r_organization_social%20rw_organization_admin%20w_member_social' \
  -H 'Postman-Token: 1c7d6199-f41b-43bc-b9ae-10d4bde0d968' \
  -H 'cache-control: no-cache'

Response contains code which needs to used in Step 2

  • Step 2

use CODE received from Step 1

curl -X GET \
  'https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code=[CODE]&redirect_uri=https://getpostman.com/oauth2/callback&client_id=[CLIENT_ID]&client_secret=[CLIENT_SECRET]' \
  -H 'Postman-Token: f9542a93-fc9d-4cc4-aa38-a10f6cf2eb6f' \
  -H 'cache-control: no-cache'

This will give you the access Token

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM