hjespers / teslams

Tesla node.js apps and javascript libraries using Tesla's HTTP interfaces
BSD 3-Clause "New" or "Revised" License
417 stars 106 forks source link

6.0 Tesla API #78

Closed arthurblake closed 9 years ago

arthurblake commented 9 years ago

Would be nice to update teslams with all the 6.0 API commands. Not sure what the implications are for the new oAuth authentication. See https://github.com/timdorr/model-s-api which was recently updated...

hjespers commented 9 years ago

Yes I have wanted to do this for some time. Most of the commands are essentially the same so updating the main login function to use bearer tokens and updating the URLs are the main changes. Then everything else should work without rewriting existing code. There are a few new functions like opening the trunk to add as well.

arthurblake commented 9 years ago

How are we going to deal with oAuth? RE: https://github.com/timdorr/model-s-api/issues/8

hjespers commented 9 years ago

I have tested out a few of the commands last night using curl..

curl -H "Content-Type: application/json" -d '{ "grant_type" : "password", "client_id" : "", "client_secret" : "", "email" : "", "password" : "" }' https://owner-api.teslamotors.com/oauth/token

This returns the JSON below with a bearer token for all subsequent requests (instead of the cookie as in the old API)

{"access_token":"e9321.......812c69","token_type":"bearer","expires_in":7776000}

You still have to call "vehicles" to get the list of vehicles and the "id" for the car you want to control.

curl -H "Authorization: Bearer e9321.......812c69" \ -H "Content-Type: application/json" \ -i https://owner-api.teslamotors.com/api/1/vehicles

returns same as before...

{"response":[{"color":null,"display_name":"TESLAMS","id": 1234567890123456789,"option_codes":"MS01,RENA,TM00,DRLH,PF00,BT60,PMMB,RFPO,WT21,IDPB,IPMB,TR00,SU01,SC01,TP01,AU01,CH00,HP00,PA01,PS00,AD02,X020,X025,X001,X003,X007,X011,X013,COUS","vehicle_id": 1234567890,"vin":"","tokens":["",""],"state":"online","remote_start_enabled":true,"calendar_enabled":true,"notifications_enabled":true}],"count":1}

HTTP GET to call /data_request much as before

curl -H "Authorization: Bearer e9321.......812c69" \ -H "Content-Type: application/json" \ -i https://owner-api.teslamotors.com/api/1/vehicles/1234567890123456789/data_request/charge_state

HTTP POST to call /command much as before

curl -H "Authorization: Bearer e9321.......812c69" \ -H "Content-Type: application/json" \ --data '{"percent" : "90"}' \ -i https://owner-api.teslamotors.com/api/1/vehicles/1234567890123456789/command/set_charge_limit

hjespers commented 9 years ago

I have teslams.all() working and returning the array of vehicles on the account. I will continue to work on the other functions over the holidays.

chaenel commented 9 years ago

Hello, I become crazy, I try to implement a datalogger for my TMS, and I can't have a valid authentication, with old API and also with new API. I can't understand what happen :(

With old method, if I do :

curl -b cookies.txt -c cookies.txt -H "Accept: application/json" -X POST -d "user_session[email]=my_email" -d "user_session[password]=my_password" https://portal.vn.teslamotors.com/login

I get an 500 error with the message "We're sorry, but something went wrong We've been notified about this issue and we'll take a look at it shortly."

And with the new method, exactly the same message with this command line : curl -H "Content-Type: application/json" -d '{ "grant_type" : "password", "client_id" : "", "client_secret" : "", "email" : "my_email", "password" : "my_password" }' https://owner-api.teslamotors.com/oauth/token

I am sure my mail/password is good because it works well with my phone app, and if with the new API I set a bad password I get :

{"response":"authorization_required"}

Any idea ? :/

hjespers commented 9 years ago

I now have all the old REST API calls working with the new 6.0 version of the API. I also ported over all of the example apps (except "restla.js") which was not too hard because I made the new API as backward compatible as possible. Still not sure if I will release this to the public given that Tesla seems to be shutting down access to the API from third party apps. Might be a dead end until Tesla publishes an official owner API or a SDK for third party apps.

hjespers commented 9 years ago

New API is implemented and on github for people to test. I have not published to npm repository yet. Looking for feedback first.