【问题标题】:How to avoid InValidClientError with requests-oauthlib token fetching?如何通过 requests-oauthlib 令牌获取避免 InValidClientError?
【发布时间】:2018-01-27 20:15:00
【问题描述】:

我正在使用来自requests-oauthlib 的标准 oauth2 网络流示例从 QuickBooks Online 的 API 获取令牌,但是虽然我已成功重定向到 QBO 站点,但已获得授权并返回代码以请求我得到的令牌一个:

oauthlib.oauth2.rfc6749.errors.InvalidClientError

当我实际发出令牌请求时出错。 google了很多,好像我没有正确授权。 QBO 说实际请求可能如下所示:

POST https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer HTTP/1.1
Accept: application/json
Authorization: Basic UTM0dVBvRDIwanp2OUdxNXE1dmlMemppcTlwM1d2
    NzRUdDNReGkwZVNTTDhFRWwxb0g6VEh0WEJlR3dheEtZSlVNaFhzeGxma1l
    XaFg3ZlFlRzFtN2szTFRwbw==
Content-Type: application/x-www-form-urlencoded
Host: oauth.platform.intuit.com
Body: grant_type=authorization_code&
code=L3114709614564VSU8JSEiPkXx1xhV8D9mv4xbv6sZJycibMUI&
redirect_uri=https://www.mydemoapp.com/oauth-redirect

我需要像这样生成授权标头:

"基本" + base64encode(client_id + ":" + client_secret)

我已经像这样创建了上面的内容:

auth_code = client_id + ":" + client_secret
auth_code = base64.b64encode(bytes(auth_code, 'utf-8'))
auth_code = auth_code.decode('utf-8')

但是在令牌代码中传递它,例如:

token = qbo.fetch_token(token_url, code=code, authorization_response=redirect_uri, headers={'Authorization' : auth_code})

导致 InvalidClientError 消息。我觉得我真的很接近,但在最后的 oauth2 障碍中失败了,但是有人可以帮我授权令牌吗?

非常感谢, 约翰

----- 编辑 1 -----

感谢 stovfl,这非常有帮助,我现在根据您的建议尝试了一些新东西。我刚刚更新了代码,使其如下所示:

headers = {'Accept':'application/json'}
headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
auth = HTTPBasicAuth(client_id, client_secret)
body = 'grant_type=authorization_code&code=' + code + '&redirect_uri=' + redirect_uri_token
token = qbo.fetch_token(token_url, auth=auth, body=body, headers=headers)

这又产生了一个错误:

ValueError:请提供代码或授权响应参数。

检查实际的 oauth 库代码,必须提供代码/身份验证响应,因此我再次更新以从回调中获取代码并在我的 fetch_token 请求中使用它:

all_args = request.args.to_dict()
code = all_args['code']
headers = {'Accept':'application/json'}
headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
auth = HTTPBasicAuth(client_id, client_secret)
body = 'grant_type=authorization_code&code=' + code + '&redirect_uri=' + redirect_uri_token
token = qbo.fetch_token(token_url, code=code, auth=auth, body=body, headers=headers)

我也试过删除代码,只在主调用中,所以主体看起来像这样:

body = 'grant_type=authorization_code' + '&redirect_uri=' + redirect_uri_token

但无论哪种方式,它现在都会回到之前的错误:

oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client)

我不知道从这里去哪里。

【问题讨论】:

    标签: python api oauth-2.0 python-requests


    【解决方案1】:

    问题:QuickBooks intuit 开发者:交换访问令牌的授权码,使用 OAut2.0。

    根据给定的Request 示例,您应该这样做:

    1. 线路:POST https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer HTTP/1.1

      POST 到https://oauth.plat... HTTP 版本1.1,默认使用OAuth2.fetch_token(...

      url_endpoint = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'
      
    2. 线路:Accept: application/json

      你必须接受application/jsonOAuth2.fetch_token(... 默认接受任何东西。 没什么可做的,但为了完整起见,您可以将其定义如下:

      headers = {'Accept':'application/json'}
      
    3. 线路:Authorization: Basic a2V5OnNlY3JldA==

      要求您在基本身份验证标头中传递身份验证凭据

      from requests.auth import HTTPBasicAuth
      auth = HTTPBasicAuth(client_id, client_secret)
      
    4. 线路:Content-Type: application/x-www-form-urlencoded

      body 数据必须通过form-urlencoded

      headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
      
    5. 线路:Host: oauth.platform.intuit.com

      用户特定,无事可做

    6. 线路:Body: grant_type=authorization_code&code=L311...&redirect_uri=https://www.mydemoapp.com/oauth-redirect

      body 中的数据放置。

      body = 'grant_type=authorization_code&code=L311...&redirect_uri=https://www.mydemoapp.com/oauth-redirect'
      
    7. 交换访问令牌的授权码

      token = qbo.fetch_token(url_endpoint, auth=auth, body=body, headers=headers)
      

    注意:无法通过 QuickBooks intuit Developer 验证,但 requests 给我以下请求消息:

    请求消息

    headers:
        Accept: application/json
        Accept-Encoding:    gzip, deflate
        Content-Length: 139
        User-Agent: python-requests/2.11.1
        Authorization:  Basic a2V5OnNlY3JldA==
        Host:   ... omited, because user specific
        Content-Type:   application/x-www-form-urlencoded
    form:
        grant_type: authorization_code
        code:   L3114709614564VSU8JSEiPkXx1xhV8D9mv4xbv6sZJycibMUI
        redirect_uri:   https://www.mydemoapp.com/oauth-redirect
    

    使用 Python:3.4.2 测试 - 请求:2.11.1

    【讨论】:

      猜你喜欢
      • 2014-09-12
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多