简体   繁体   English

如何使用 express 和 axios 从 Linkedin API 获取访问令牌?

[英]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.我正在研究 Linkedin API 以使用“使用 Linkedin 登录”功能。 I am using axios to get the accessToken but I am getting this error: error: 'invalid_redirect_uri',我正在使用 axios 获取 accessToken 但我收到此错误:错误:'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.问题是,虽然在 Postman 上,redirect_uri 完全相同,但它可以工作并且我得到了访问令牌,在快递上我得到了上述错误。

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.无法弄清楚究竟是什么错误。

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. redirect_uri 仅在您请求授权码时才需要编码,但当您请求访问令牌时,您使用重定向 URL 而不对其进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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