Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register

Tesla API Token Generator

Since there seemed to be a bit of disorder on how, exactly, to get Tesla API tokens to be used to securely use 3rd party applications, I decided to take it upon myself to create a (very) simple python script that will generate and print out a token, given a correct username and password, along with what day and time the token will expire.

Right now it requires a little technical knowledge in that python must be installed along with the python requests package, and the script must be run in a terminal. But no actual code needs to be written by the user. Usage is as follows:

python teslatoken.py -e <mytesla email> -p <mytesla password>

I don't have much experience doing so, but going forward I may try to package it up in a nicer way to reduce the need for technical knowledge.

For now the code is all there, so everyone can feel free to double-check it and verify it's not malicious or anything. It's just a pretty straight forward requests call to the Tesla owner API, some datetime formatting, and handling of different error response codes.
 

Attachments

  • teslatoken.zip
    1,012 bytes · Views: 1,273

Naonak

Member
Dec 22, 2015
798
1,281
Kansas
You must have some odd libraries installed with your Python with relation to json.

Traceback (most recent call last):
File "teslatoken.py", line 25, in <module>
GetToken(args.email, args.password)
File "teslatoken.py", line 6, in GetToken
req = requests.post('https://owner-api.teslamotors.com/oauth/token', json={'grant_type': 'password', 'client_id': 'e4a9949fcfa04068f59abb5a658f2bac0a3428e4652315490b659d5ab3f35a9e', 'client_secret': 'c75f14bbadc8bee3a7594412c31416f8300256d7668ea7e6e7f06727bfb9d220', 'email': username, 'password': password})
File "/usr/lib/python3/dist-packages/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
TypeError: request() got an unexpected keyword argument 'json'


Also, you are using Python 2 (nothing wrong with that), but if you want it to work with Python 3, add some () around your print statements.
 
You must have some odd libraries installed with your Python with relation to json.

Traceback (most recent call last):
File "teslatoken.py", line 25, in <module>
GetToken(args.email, args.password)
File "teslatoken.py", line 6, in GetToken
req = requests.post('https://owner-api.teslamotors.com/oauth/token', json={'grant_type': 'password', 'client_id': 'e4a9949fcfa04068f59abb5a658f2bac0a3428e4652315490b659d5ab3f35a9e', 'client_secret': 'c75f14bbadc8bee3a7594412c31416f8300256d7668ea7e6e7f06727bfb9d220', 'email': username, 'password': password})
File "/usr/lib/python3/dist-packages/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
TypeError: request() got an unexpected keyword argument 'json'


Also, you are using Python 2 (nothing wrong with that), but if you want it to work with Python 3, add some () around your print statements.

Hmmm looks like you may have an older version of requests? It looks like the json parameter was added in 2.4.2. And yes, using Python 2.

I'll upload a new version fixed for both of those.
 
  • Like
Reactions: Naonak
Hmmmmmm I can't seem to find a way to edit my first post. I added parentheses around the print statements which, I think, should fix things on python 3(don't have an install of that handy though) and changed the json parameter to data, which should work on older requests versions. Looks like it still happily gives back the token with that change, so no harm.
 

Attachments

  • teslatoken.zip
    1,010 bytes · Views: 538
  • Like
Reactions: Naonak
A naive question: for those of us unaccustomed to the Tesla API, what's the difference between API tokens and the login credentials in terms of capabilities? Naturally, I get that sharing your password is undesirable and is avoided by using a token instead, but does the holder of a token have all of the capabilities of someone with the password or a subset?

Thanks in advance.
 
Also, the token alone does not allow to start the car (still need the password in addition to do that). So it is a little bit less dangerous to give out to 3rd party services. With someone stealing username/password, you are giving literally out the car's location, a way to unlock it without keys, and also start and drive it without keys.
 
Last edited:
Hey rog, when I researched the Tesla api, the only command I saw that indicated it needed a password (other than for getting a token) was the remote start command here Tesla Model S JSON API · Apiary. Is that documentation out of date?

Otherwise, the token can do anything else, like unlock, get position, open sunroof, etc.

Edit - sorry...nevermind, I read your response incorrectly.
 
Last edited:
Thanks. I already installed Python 3.6 on a Windows 8.1 PC and downloaded the Teslatoken.py script that was uploaded by MarcusMaximus. I launched Python 3.6 in the GUI mode and opened the Teslatoken.py script. I know I need to add my Tesla log in credentials to the script but I do not know where to place it in the script. I'm assuming once I add my log in credentials to the script I simply run the script and get an access token.

 
As another alternative for getting a token one can use Postman.

postman_tesla.png
 
  • Like
Reactions: ishareit
You can run a simple curl command in the Terminal as follows. Just update the email and password value.

Code:
curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "grant_type=password" -F "client_id=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384" -F "client_secret=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3" -F "email=USER_YOUR_EMAIL" -F "password=USER_YOUR_PASSWORD" "https://owner-api.teslamotors.com/oauth/token"
 

Products we're discussing on TMC...

About Us

Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.

Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.


SUPPORT TMC
Top