def get_channel(self):
#first get a token we need to sign in order to prove we are who we say we are
r = requests.get(str(self.base_link_uri) + "/get_device_token", params={"UUID": self.uuid, })
token = r.json()["token"]
# get the private Key
with open(CloudLinkSettings.PRIVATE_KEY_LOCATION,'r') as key_file:
private_key = serialization.load_pem_private_key(key_file.read(),
password=CloudLinkSettings.PRIVATE_KEY_PASSPHRASE,
backend=default_backend())
# sign the token with our private key
signer = private_key.signer(padding.PKCS1v15(), hashes.SHA256())
signer.update(bytes(token))
signature = signer.finalize()
# get the randomly assigned channel for my UUID
r = requests.get(str(self.base_link_uri) + "/get_device_group",
params={"UUID": self.uuid, "signature": b64encode(signature), "format": "PKCS1_v1_5"})
if r.ok:
self.channel_uri = r.json()["channel"]
elif r.status_code == 400:
raise Exception("UUID or Token not registered with Cloud.")
elif r.status_code == 403:
raise Exception("Signature didn't verify correctly. Bad private key or signature.")
评论列表
文章目录