Auto renewal of auth token for RaspberryPi agent

revert-dabc3590
Savidude 7 years ago
parent d148192426
commit 3ec0e1bdf3

@ -100,6 +100,13 @@ def on_disconnect(client, userdata, rc):
agent_connected = False agent_connected = False
print ("Agent disconnected from broker") print ("Agent disconnected from broker")
print("Obtaining new access token")
token = RefreshToken()
response = token.updateTokens()
newAccessToken = response['access_token']
client.username_pw_set(newAccessToken, password="")
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The Main method of the server script # The Main method of the server script
# This method is invoked from RaspberryStats.py on a new thread # This method is invoked from RaspberryStats.py on a new thread

@ -1,7 +1,8 @@
import httplib
import json import json
import urllib import urllib
import iotUtils import iotUtils
import requests
applicationKey = None applicationKey = None
refreshToken = None refreshToken = None
@ -10,11 +11,11 @@ filename = "deviceConfig.properties"
class RefreshToken(): class RefreshToken():
def post(self, base, port, url, payload, appKey): def post(self, url, payload, appKey):
conn = httplib.HTTPConnection(base, port, timeout=60) headers = { 'Authorization' : 'Basic ' + appKey, 'Content-Type' : 'application/x-www-form-urlencoded' }
conn.request('POST', url, payload, { 'Authorization' : 'Basic '+appKey, 'Content-Type' : 'application/x-www-form-urlencoded' }) baseUrl = iotUtils.HTTP_EP + url
r = conn.getresponse() response = requests.post(baseUrl, params=payload, headers=headers);
return r.read() return response
def read_server_conf(self): def read_server_conf(self):
with open(filename, 'r') as outfile: with open(filename, 'r') as outfile:
@ -46,7 +47,7 @@ class RefreshToken():
params = urllib.urlencode({"grant_type": "refresh_token", "refresh_token": refreshToken, params = urllib.urlencode({"grant_type": "refresh_token", "refresh_token": refreshToken,
"scope": "Enroll device"}) "scope": "Enroll device"})
data = self.post("localhost", 8280, "/token", params,applicationKey) data = self.post("/token", params, applicationKey)
response = json.loads(data) response = json.loads(data)
self.updateFile(response) self.updateFile(response)
return response return response
Loading…
Cancel
Save