Python OAuth 2.0 --> Fitbit API "invalid_client" 错误

Python OAuth 2.0 --> Fitbit API "invalid_client" error

我正在尝试将 OAuth 2.0 与 Fitbit 网络一起使用 API,但是我一直收到返回给我的错误消息。

我得到的错误是:

{"errors":[{"errorType":"invalid_client","message":"Invalid authorization header format. The client id was not provided in proper format inside Authorization Header. Received authorization header = Basic XXXXXXXXXXXXXXXXXXXXXXX, received client encoded id = XXXXXX. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}

我在网上搜索了几个小时,导致此类错误的最常见原因是 "Basic" 拼写不正确,但我认为这里的情况并非如此,因为错误更具体地指出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)

有什么想法吗?

您的授权 header 部分有误。而不是

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

您需要先连接,然后进行 base64 编码

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

这在 "Access Token Request" 章的 docs 中有描述。