I am developing a dirty and ugly python api from PHP code.
For now, I can get the tokens and the device information.
import requests
class HonClient:
id_token = ''
cognito_token = ''
username = ''
password = ''
def __init__(self, username, password):
self.username = username
self.password = password
def update_tokens(self):
client = requests.Session()
response = client.post(
'https://he-accounts.force.com/SmartHome/s/sfsites/aura?r=3&other.LightningLoginCustom.login=1', data={
"message": '{"actions":[{"id":"87;a","descriptor":"apex://LightningLoginCustomController/ACTION$login","callingDescriptor":"markup://c:loginForm","params":{"username":"' + self.username + '","password":"' + self.password + '","startUrl":"/SmartHome/setup/secur/RemoteAccessAuthorizationPage.apexp"}}]}',
"aura.context": '{"mode":"PROD","fwuid":"2yRFfs4WfGnFrNGn9C_dGg","app":"siteforce:loginApp2","loaded":{"APPLICATION@markup://siteforce:loginApp2":"PnuMahsrn7JWWgS2n6sUkQ"},"dn":[],"globals":{},"uad":false}',
"aura.pageURI": '/SmartHome/s/login/?language=en&startURL=%2FSmartHome%2Fsetup%2Fsecur%2FRemoteAccessAuthorizationPage.apexp',
"aura.token": "null"
})
url = response.json()['events'][0]['attributes']['values']['url']
html = client.get(url).text
url = html[html.index('https'):html.index('"', html.index('https'))]
client.get(url)
oauth = client.get(
'https://he-accounts.force.com/SmartHome/services/oauth2/authorize?response_type=token+id_token&client_id=3MVG9QDx8IX8nP5T2Ha8ofvlmjLZl5L_gvfbT9.HJvpHGKoAS_dcMN8LYpTSYeVFCraUnV.2Ag1Ki7m4znVO6&redirect_uri=hon%3A%2F%2Fmobilesdk%2Fdetect%2Foauth%2Fdone&display=touch&scope=api%20openid%20refresh_token%20web&nonce=82e9f4d1-140e-4872-9fad-15e25fbf2b7c').text
self.id_token = oauth[oauth.index("id_token") + 9:oauth.index('&', oauth.index('id_token'))]
self.cognito_token = client.post("https://api-iot.he.services/auth/v1/login", json={
"appVersion": "1.39.2",
"mobileId": "xxxxxxxxxxxxxxxxxx",
"osVersion": 30,
"os": "android",
"deviceModel": "goldfish_x86"
}, headers={'id-token': self.id_token}).json()['cognitoUser']['Token']
def _execute_request(self, url, params):
response = requests.get(url, params=params, headers={
'cognito-token': self.cognito_token,
'id-token': self.id_token
})
if response.status_code == 401:
self.update_tokens()
response = requests.get(url, params=params, headers={
'cognito-token': self.cognito_token,
'id-token': self.id_token
})
return response.json()
def get_devices(self):
return self._execute_request('https://api-iot.he.services/commands/v1/appliance', {})['payload']['appliances']
def get_status(self, device):
return self._execute_request('https://api-iot.he.services/commands/v1/context?category=CYCLE',
params={
'applianceType': device['applianceTypeName'],
'macAddress': device['macAddress']
})
client = HonClient("[email protected]", "mypassword")
devices = client.get_devices()
info = client.get_status(devices[0])