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

MarcusMaximus

Active Member
Jan 2, 2017
3,789
16,514
Los Gatos
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,083

Naonak

Member
Dec 22, 2015
796
1,153
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.
 

MarcusMaximus

Active Member
Jan 2, 2017
3,789
16,514
Los Gatos
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

Naonak

Member
Dec 22, 2015
796
1,153
Kansas
Hmm... might be the age of my Python 2 installation. I need to build a new dev box and will test with 2.7.x branch... does not work in the 2.6.x branch. But curiously, it also has the same problem with the 3.4.x branch of Python3, so I'm not convinced.
 
  • Helpful
Reactions: Hbrink

Naonak

Member
Dec 22, 2015
796
1,153
Kansas
Haha guess we cross posted. Yeah, I'm inclined to agree with you. I need to build out a new dev environment anyway, so this is a good a reason as any! :)
 

MarcusMaximus

Active Member
Jan 2, 2017
3,789
16,514
Los Gatos
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: 461
  • Like
Reactions: Naonak

Naonak

Member
Dec 22, 2015
796
1,153
Kansas
Yep... 2.7.6 does not work, but 2.7.12 does work.

I just tested your new version on 2.7.6 and Python3. Works great!

PS - the edit ability goes away after like 5 minutes. Kinda goofy, but that's the way the forum is set up.
 

somnambule

Member
Aug 26, 2016
215
263
SF Bay Area
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.
 

Naonak

Member
Dec 22, 2015
796
1,153
Kansas
Yes, the Token is as good as your password as far as vehicle control goes... the difference is that tokens expire after 90 days and/or your credentials can be changed and expire the token immediately.
 

rog

Member
May 30, 2015
295
158
Connecticut
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:

luckyj

Member
Dec 8, 2016
425
502
Northern Virginia, USA
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:

Frankman60

Member
Jun 21, 2016
417
86
San Diego, CA
I downloaded the Teslatoken.py file to try to get an access token for use with Tesla Cockpit. Now if I can only figure out how to run it on my Windows 8.1 PC:(. Any assistance would be appreciated.
 

Frankman60

Member
Jun 21, 2016
417
86
San Diego, CA
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.

 

Newport Ryan

Member
Jul 14, 2016
85
38
Newport Beach, CA
As another alternative for getting a token one can use Postman.

postman_tesla.png
 
  • Like
Reactions: sumitkgarg

t3slaMS

Member
Jan 18, 2017
28
11
California
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"
 

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