使用GitHub OAuth2时获取访问令牌时出现问题

tquggr8v  于 2022-09-21  发布在  Git
关注(0)|答案(1)|浏览(320)

我正在尝试遵循GitHub步骤将用户重定向回您的站点

我能够获得授权码,但我在将其转换为访问令牌时遇到了麻烦。

这是带有授权码的调用

http://localhost:8080/login/oauth2/code/github?
code=1e16b9f5e7e1b63ce1d4
&state=nS7zT9elsOzMmf6SKUXJz6m_Z-PBvhfUfObZiR3UhWI%3D

我正在尝试将该授权码交换为访问令牌。我尝试在参数中使用和不使用GRANT_TYPE。下面的curl命令有什么明显的错误吗?

curl --location --request POST 'https://github.com/login/oauth/access_token' 
--header 'Content-Type: application/x-www-form-urlencoded' 
--header 'Cookie: user_session=omYLX_QFvwdlluotRIJVi_mz7v_FQaIdQBZtkELBlBWBkhnj; __Host-user_session_same_site=omYLX_QFvwdlluotRIJVi_mz7v_FQaIdQBZtkELBlBWBkhnj' 
--data-urlencode 'client_id=xxxxx' 
--data-urlencode 'client_secret=xxxxx' 
--data-urlencode 'code=1e16b9f5e7e1b63ce1d4' 
--data-urlencode 'redirect_uri=http://localhost:8080/login/oauth2/code/github' 
--data-urlencode 'state=nS7zT9elsOzMmf6SKUXJz6m_Z-PBvhfUfObZiR3UhWI%3D' 
--data-urlencode 'grant_type=authorization_code'

这是我收到的错误消息。

error=bad_verification_code&error_description=The+code+passed+is+incorrect+or+expired.&error_uri=https%3A%2F%2Fdocs.github.com%2Fapps%2Fmanaging-oauth-apps%2Ftroubleshooting-oauth-app-access-token-request-errors%2F%23bad-verification-code
ecfsfe2w

ecfsfe2w1#

我认为您在请求中发送了不必要的参数。而与OAuth令牌交换身份验证码的参数如下

  • 应用凭证(CLIENT_ID+CLIENT_ASSET)
  • 代码

我现在还没有cURL示例,但下面是我的python脚本中的示例,我在其中执行相同的操作,例如,使用OAuth令牌交换Authcode

data = {
            'client_id': settings.CLIENT_ID,
            'client_secret': settings.CLIENT_SECRET,
            'code': self.code,
        }
 headers = {'Accept': 'application/json'}
 response = requests.post(
       'https://github.com/login/oauth/access_token',
       data=data,
       headers=headers)

希望能有所帮助

相关问题