connection to the api in python3

Hello,
I can't connect to the API, I retrieved my connection information via the application (token and secret) but I got the 401 error, here is my script:
Thanks for your help !

#!/usr/bin/env python3

import time
import hashlib
import hmac
import base64
import switchcloud_jeremy
import sys
import logging
import json
import requests

# open token 
token = '48406.................................................................f0158bb7501' # copy and paste from the SwitchBot app V6.14 or later
# secret key
secret = 'XXXXXXXXXXXXXXXX' # copy and paste from the SwitchBot app V6.14 or later
nonce = 'fa98a185-007c-4418-a4a5-a62f75f26aca'
t = int(round(time.time() * 1000))
string_to_sign = '{}{}{}'.format(token, t, nonce)
string_to_sign = bytes(string_to_sign, 'utf-8')
secret = bytes(secret, 'utf-8')
sign = base64.b64encode(hmac.new(secret, msg=string_to_sign, digestmod=hashlib.sha256).digest())
sign =sign.upper();
print ('Authorization: {}'.format(token))
print ('t: {}'.format(t))
print ('sign: {}'.format(str(sign, 'utf-8')))
print ('nonce: {}'.format(nonce))

header={'Authorization' :format(token),'t' :format(t),'sign' :format(str(sign, 'utf-8')),'nonce' :format(nonce), 'content-type' : 'application/json; charset=utf8'}
print(header)
header={'Authorization' :token,'t' :str(t),'sign' :str(sign, 'utf-8'),'nonce' :nonce, 'content-type' : 'application/json; charset=utf8'}
print(header)
r = requests.get("https://api.switch-bot.com/v1.1/devices", params = {}, headers = header)

if r.status_code != 200:
    print(r.status_code)
else:
    print(r.json())
 

 

0

コメント

1件のコメント
  • same issue here. Just got my API Key in the app, trying to access Switch Bot API-s (be it using python, C#, or even curl), getting 401 on all of them. Tried bot SwaggerHub API v1.0 and 1.1, both return 401

    0

サインインしてコメントを残してください。

お探しのものを見つけられませんでしたか?

新規投稿