【问题标题】:Python OAuth 2.0 --> Fitbit API "invalid_client" errorPython OAuth 2.0 --> Fitbit API“invalid_client”错误
【发布时间】:2018-03-04 00:34:06
【问题描述】:

我正在尝试将 OAuth 2.0 与 Fitbit Web API 一起使用,但我不断收到错误消息。

我得到的错误是:

{"errors":[{"errorType":"invalid_client","message":"无效的授权标头格式。在授权标头中未以正确格式提供客户端 ID。收到的授权标头 = 基本 XXXXXXXXXXXXXXXXXXXXXXX,收到客户端编码 id = XXXXXX。访问 https://dev.fitbit.com/docs/oauth2 了解有关 Fitbit Web API 授权过程的更多信息。"}],"success":false}

我已经在网上搜索了几个小时,而导致此类错误的最常见原因是“基本”拼写不正确,但我认为这里的情况并非如此,因为该错误更具体地指出了 client_id 不是以适当的格式提供。

以下是我的代码的相关部分:

#These are the secrets etc from Fitbit developer
client_id = "XXXXX"
client_secret = "XXXXXXXXXXXXXXXXXXX"

# Encode OAuthTwoClientID and ClientOrConsumerSecrets and convert from strings to bytes
b64id = base64.b64encode(client_id.encode())
b64secret = base64.b64encode(client_secret.encode())

# Pass encoded ID and Secrets to header and decode 
header = {'Authorization': 'Basic ' + b64id.decode() + ":" + b64secret.decode(), 
'Content-Type': 'application/x-www-form-urlencoded'}

# Start the request
req = requests.get(TokenURL,BodyURLEncoded, headers=header)

有什么想法吗?

【问题讨论】:

    标签: python-3.x oauth-2.0 base64 python-requests fitbit


    【解决方案1】:

    您的授权标头部分错误。而不是

    'Basic ' + b64id.decode() + ":" + b64secret.decode(),
    

    您需要先连接,然后使用 base64 编码

    secret = client_id + ":" + client_secret
    b64secret = base64.b64encode(secret.encode())
    header = { 'Authorization': 'Basic ' + b64secret.decode() }
    

    这在“访问令牌请求”一章中的docs 中有描述。

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多