Python Linkedin身份验证提供无效的重定向uri

Python Linkedin身份验证提供无效的重定向uri,python,oauth-2.0,python-requests,linkedin,Python,Oauth 2.0,Python Requests,Linkedin,我正在使用linkedin认证api登录我的网站用户 首先,我将重定向到以下url: 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%

我正在使用linkedin认证api登录我的网站用户

首先,我将重定向到以下url:

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应用程序授予权限后,用户将成功重定向到重定向uri,并附加代码参数

之后,我发送一个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'}

正如您看到的,重定向uri在两个请求中都是相同的…

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

您需要发送

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

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

您需要发送

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

为访问令牌交换授权码通常不需要重定向uri。虽然我不知道linkedin是否如此。如果你不发送重定向uri,它能工作吗?是的,它需要基于linkedin文档交换你的授权码以获得访问令牌通常在没有重定向uri的情况下完成。虽然我不知道linkedin是否如此。如果你不发送重定向uri,它能工作吗?是的,它需要基于linkedin文档。非常感谢Hanks@HansZ通过使用类似$.ajax{url:,method:'post',headers:{ContentType:'application/x-www-form-urlencoded'},success:functionresp{console.logresp;}的ajax post方法来实现这一点;谢谢Hanks@HansZ通过使用类似$.ajax{url:,method:'post',headers:{ContentType:'application/x-www-form-urlencoded'},success:functionresp{console.logresp;}的ajax post方法来实现这一点;