Here's the code. I removed the libraries and made it all functions, changed the switches to static variables at the top you'll need to set with your information. It's very repetitive now but it'll make it easier to understand. The workflow is pretty simple.
Check if there is an encrypted token saved
Generate one, encrypt it and save it locally if there isn't one already
Decrypt the token from file and verify it hasn't expired
Query the state of the vehicle (quit if asleep)
Write results to SQL
Let me know if you get stuck.
EDIT: Forums is doing some weird stuff with the PasteBin link (Remove the spaces from below)
h t t p s : / / pastebin.com/NxJs7i8M
EDIT on the EDIT: I also forgot to mention, I have an S so i'm only saving the metrics I care about to SQL, I know the API returns more than is being written but I simplified the code to where you can easily add more metrics for an X or 3 if you wanted to, oh and change the encryption key after you save it locally since it's posted on the internet (just update the numbers on the encryption and decryption functions).