TMC is an independent, primarily volunteer organization that relies on ad revenue to cover its operating costs. Please consider whitelisting TMC on your ad blocker and becoming a Supporting Member. For more info: Support TMC
Start a Discussionhttps://teslamotorsclub.com/tmc/tags/

Tesla API Token Generator

Discussion in 'Model S' started by MarcusMaximus, Jan 6, 2017.

Tags:
  1. MarcusMaximus

    MarcusMaximus Member

    Joined:
    Jan 2, 2017
    Messages:
    592
    Location:
    San Jose
    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.
     

    Attached Files:

    • Like x 4
  2. Naonak

    Naonak Member

    Joined:
    Dec 22, 2015
    Messages:
    499
    Location:
    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.
     
  3. MarcusMaximus

    MarcusMaximus Member

    Joined:
    Jan 2, 2017
    Messages:
    592
    Location:
    San Jose
    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 x 1
  4. Naonak

    Naonak Member

    Joined:
    Dec 22, 2015
    Messages:
    499
    Location:
    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.
     
  5. Naonak

    Naonak Member

    Joined:
    Dec 22, 2015
    Messages:
    499
    Location:
    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! :)
     
  6. MarcusMaximus

    MarcusMaximus Member

    Joined:
    Jan 2, 2017
    Messages:
    592
    Location:
    San Jose
    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.
     

    Attached Files:

    • Like x 1
  7. Naonak

    Naonak Member

    Joined:
    Dec 22, 2015
    Messages:
    499
    Location:
    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.
     
  8. somnambule

    somnambule Member

    Joined:
    Aug 26, 2016
    Messages:
    207
    Location:
    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.
     
  9. Naonak

    Naonak Member

    Joined:
    Dec 22, 2015
    Messages:
    499
    Location:
    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.
     
    • Informative x 1
    • Disagree x 1
  10. rog

    rog Member

    Joined:
    May 30, 2015
    Messages:
    294
    Location:
    Connecticut
    #10 rog, Jan 6, 2017
    Last edited: Jan 6, 2017
    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.
     
    • Informative x 2
  11. luckyj

    luckyj Member

    Joined:
    Dec 8, 2016
    Messages:
    108
    Location:
    Virginia, USA
    #11 luckyj, Jan 6, 2017
    Last edited: Jan 6, 2017
    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.
     
  12. somnambule

    somnambule Member

    Joined:
    Aug 26, 2016
    Messages:
    207
    Location:
    SF Bay Area
    Thanks, Naonak and rog.
     
  13. Carspotter Daily

    Carspotter Daily Tesla > ICE

    Joined:
    Aug 12, 2015
    Messages:
    73
    Location:
    Hamilton, Ontario, Canada
    Thank you very much for this,
    also thanks to luckj
    I might have something coming soon...
     
  14. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,128
    Location:
    Menlo Park
    I added in a flag to the command line tool in teslams to print a token.

    Code:
    teslacmd -u [email protected] -p pa55w0rd --print_token
     
    • Like x 1
  15. Frankman60

    Frankman60 Member

    Joined:
    Jun 21, 2016
    Messages:
    183
    Location:
    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.
     
  16. Newport Ryan

    Newport Ryan Supporting Member

    Joined:
    Jul 14, 2016
    Messages:
    86
    Location:
    Newport Beach, CA
  17. Frankman60

    Frankman60 Member

    Joined:
    Jun 21, 2016
    Messages:
    183
    Location:
    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.

     
  18. GMDC

    GMDC Member

    Joined:
    Nov 18, 2016
    Messages:
    25
    Location:
    Switzerland
    I'm getting 701 error, event not found. But can't find that in the code.
    Any idea? Seems to me it doesn't like the @ in the command line (username as example). Without it works, but fails to log of course. I'm on OSX
     
  19. Newport Ryan

    Newport Ryan Supporting Member

    Joined:
    Jul 14, 2016
    Messages:
    86
    Location:
    Newport Beach, CA
    As another alternative for getting a token one can use Postman.

    [​IMG]
     
    • Like x 1
  20. t3slaMS

    t3slaMS Member

    Joined:
    Jan 18, 2017
    Messages:
    24
    Location:
    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"
     
    • Helpful x 1

Share This Page