【问题标题】:Linkedin authentication giving invalid redirect_uriLinkedin 身份验证给出无效的redirect_uri
【发布时间】:2017-11-04 18:56:31
【问题描述】:

我正在使用linkedin身份验证api登录我的网站用户。

首先我重定向到以下网址:

https://www.linkedin.com/oauth/v2/authorization?' \
      'response_type=code' \
      '&client_id=*****' \
      '&redirect_uri=http%3A%2F%2F127.0.0.1%3A8000%2Faccounts%2Flinkedin%2Fcallback%3Fnext%3D%2Fhome' \
      '&state=123456789' \
      '&scope=r_basicprofile%20r_emailaddress'

这将成功重定向到linkedin 身份验证对话框。在接受向linkedin应用程序授予权限后,用户成功重定向到redirect_uri,并附加了code参数。

之后我发送一个 POST 请求,如下所示:

requests.post(
    'https://www.linkedin.com/oauth/v2/accessToken',
    data={
        'grant_type': 'authorization_code',
        'code': returned code in previous step,
        'redirect_uri': 'http%3A%2F%2F127.0.0.1%3A8000%2Faccounts%2Flinkedin%2Fcallback%3Fnext%3D%2Fhome',
        'client_id': ****,
        'client_secret': settings.LINKEDIN_CLIENT_SECRET,
    }
)

但是这个请求不成功,我不知道为什么!以下是linkedin返回的响应:

{'error': 'invalid_redirect_uri', 'error_description': 'Unable to retrieve access token: appid or redirect uri or code verifier does not match authorization code or authorization code expired'}

正如你所见,redirect_uri 在两个请求中都是相同的...

【问题讨论】:

  • 用你的授权码交换一个访问令牌通常不需要redirect_uri。 (虽然我不知道linkedin是否是这种情况。)如果您不发送redirect_uri,它会起作用吗?
  • 是的,它需要基于linkedin文档

标签: python oauth-2.0 python-requests linkedin


【解决方案1】:

在您的令牌请求中,您在 POST 数据中提供重定向 URI 的 URL 编码值,但是 requests.post 将自动对 data 对象中的键/值对进行 URL 编码。因此该值到达服务器双重编码,因此与授权请求中发送的原始值不匹配;服务器响应invalid_redirect_uri

你需要发送

    'redirect_uri': 'http://127.0.0.1:8000/accounts/linkedin/callback?next=/home',

【讨论】:

  • 非常感谢汉克斯
  • @Hans Z 有没有办法通过像 $.ajax({ url:'linkedin.com/oauth/v2/accessToken/…', method : 'POST', headers:{ContentType: 'application/ x-www-form-urlencoded'}, 成功 : function(resp) { console.log(resp); } });
猜你喜欢
  • 1970-01-01
  • 2023-02-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 2021-09-21
  • 2019-11-03
  • 2015-07-02
  • 2023-03-07
相关资源
最近更新 更多