Python Examples for API Calls now that Authorisation is sorted #301
Replies: 1 comment 13 replies
-
@Pyromancer579 Here are the only two API calls I am currently making with the new token system: def GetProducts(access_token):
url="https://owner-api.teslamotors.com/api/1/products"
headers = {"Authorization": "Bearer "+access_token}
payload={}
r = requests.get(url, data=json.dumps(payload), headers=headers)
if r.status_code!=200:
a="Error: http response "+str(r.status_code)+" "+r.reason
return a
else:
a=json.loads(r.content.decode('utf-8'))
response=a['response']
return response
def GetSolarHistory(access_token,site_id,date):
url=f"https://owner-api.teslamotors.com/api/1/energy_sites/{site_id}/calendar_history"
headers = {"Authorization": "Bearer "+access_token}
payload={}
kind='power'
period='day'
end_date = date.replace(hour=7, minute=59, second=59)+datetime.timedelta(days=1)
end_date=end_date.isoformat()
params={'kind':kind,'period': period}
params['end_date']=end_date+'Z'
r = requests.get(url, data=json.dumps(payload), headers=headers, params=params)
if r.status_code!=200:
a="Error: http response "+str(r.status_code)+" "+r.reason
else:
a=json.loads(r.content.decode('utf-8'))
return a['response'] All API calls work the same in the new system as the old system. You just have to pass a new access token. It looks like your old calls did something with the token expiration date. Maybe you were checking to see if the token needed refreshing each time? You could just delete those parts of the code. My implementation is to check once a day to refresh tokens, then when making API calls, just assume the tokens are good. Alternatively, you could try the access token, and only try to refresh if it doesn't work. I would be curious what the structure of self.token in your code used to be, as it had a ['expires_in'] attribute previously. You will need to feed your API the same format now. |
Beta Was this translation helpful? Give feedback.
-
With lots of help, I have managed to implement @timdorr 's work thanks to @skimike02 and @dvermagithub and it looks like the tokens now work for me.
But the code I had previously been using for API calls to control my Powerwall (switch its modes) when my conditions were met, doesn't seem to work, I've put the code below and the response I get:
File "/home/pi/ZappiPowerwall/QuaterHourCheck.py", line 258, in Boost
energy_sites = await client.list_energy_sites()
File "/home/pi/.local/lib/python3.7/site-packages/tesla_api/init.py", line 106, in list_energy_sites
return [_class(self, products['energy_site_id']) for products in await self.get('products')]
File "/home/pi/.local/lib/python3.7/site-packages/tesla_api/init.py", line 79, in get
await self.authenticate()
File "/home/pi/.local/lib/python3.7/site-packages/tesla_api/init.py", line 67, in authenticate
expiry_time = timedelta(seconds=self.token['expires_in'])
KeyError: 'expires_in'
Can anyone point me in the direction of some example Python API calls for use with the new token system? so I can edit my code and make it work again.
Beta Was this translation helpful? Give feedback.
All reactions