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

Model S REST API

This site may earn commission on affiliate links.
I have had success with the REST interfaces. However, when calling the streaming interface I am receiving a 401 Unauthorized error return. Here is an example code snippet:

Code:
var options =
            {
                url: "[URL]https://streaming.vn.teslamotors.com/stream/[/URL]" + vehicleId + "/?values=speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range",
                responseType: "json",
                type: "GET",
                user: username,
                password: token
            };
       // the error function is always called with 401 result
       WinJS.xhr(options).done();

Any insights appreciated. I pass in the first token received as token. Probably something simple but I am not seeing it.

Probably you are using the wrong vehicle ID. Are you using the short "id" value used in the REST API or the longer "vehicle_id"?
 
The long one (10 digits). The "vehicle_id" property returned from the /vehicles call.

Is it possible that the token returned by the vehicles call has expired? Is a call to wake_up always required to ensure new tokens? If so that could be the cause. Otherwise I am at a loss.
 
Last edited:
The long one (10 digits). The "vehicle_id" property returned from the /vehicles call.

Is it possible that the token returned by the vehicles call has expired? Is a call to wake_up always required to ensure new tokens? If so that could be the cause. Otherwise I am at a loss.

The tokens expire every 15 minutes. Actually it's more complicated than that. They expire at exactly :00 :15 :30, and :45 minutes past the hour. Both tokens are valid and they roll over ever 15 minutes so if you use the most recent token it will be good for up to 30 minutes max. Calling wake_up does not generate new tokens but it will reveal the ones that are already there.
 
I noticed from earlier messages the use of an "Authorization" header and also base64 encoding. Are either of those required? Trying to make sense of the fiddler trace.

OK, problem solved. Did my research on Basic HTTP Auth headers and that showed the problem. To help others here is what was needed:
Code:
        var options =
            {
                url: TeslaMS.streaming + vid + "/?values=speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range",
                responseType: "json",
                type: "GET",
                [COLOR=#008000]headers: { Authorization: "Basic " + Base64.encode(username + ":" + token) }[/COLOR]
            };
 
Last edited:
I have tried wake_up, climate_state, charge_state and vehicle_state.
I also tried putting in a delay if it returns 408 and then sending any of the above.
My program used to work. The only change I am aware of is that my car's software has gone recently to v5.8.8
And this was after my program worked without change in the past (heard that one before :wink:)
But I am not sure whether that was the release before v5.8.6.
The program was definately working on the 12th February because I used it to check where the car was that morning as I had to take it into a repair shop to have the windscreen replaced.
I see from your posts that you have the Rest API working at present so, if you don't mind me asking, what version of software does your Model S have ?
 
Way back when there was a shell script posted which uses curl to access the car.
I have (had) a cron job which texted me if the car wasn't charging at certain times
(I frequently forget to plug in when I get home). When sleep mode was introduced
this script stopped working and I started playing around with it (looping instead of
failing, etc). But time constraints have kept me from perfecting it.

Does anyone have an updated curl-based shell script which has the appropriate
handling of a sleeping car? Is there a better solution out there?

Thanks,
/Ed
 
Since 5.8.8 I cannot get my car out of "asleep" status.
I used to be able to talk remotely to the car and by trying various commands get it online.
Anyone had the same problem ? Any ideas about what has changed ?

I tested this today just to check. My car is running 5.8.8. Wakeup is working fine but the tokens seem to take longer to be revealed and are not necessarily always there on the first response that returns "state": "online".

I have changed tokens, ids, and vin for obvious reasons.

Code:
[FONT=Andale Mono][~] $ teslacmd -a
Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD[/FONT]
[FONT=Andale Mono]'[{"color":null,"display_name":null,"id":123,"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","user_id":123,"vehicle_id":1234567890,"vin":"1A2B3CD5E6F7G8H9I","tokens":[],"state":"asleep"}]'[/FONT]
[FONT=Andale Mono][~] $ teslacmd -w[/FONT]
[FONT=Andale Mono]Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD[/FONT]
[FONT=Andale Mono][Error: 408: {"error":"vehicle unavailable (408)"}][/FONT]
[FONT=Andale Mono][~] $ teslacmd -a[/FONT]
[FONT=Andale Mono]Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD[/FONT]
[FONT=Andale Mono]'[{"color":null,"display_name":null,"id":187,"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","user_id":220,"vehicle_id":1234567890,"vin":"1A2B3CD5E6F7G8H9I","tokens":[],"state":"asleep"}]'[/FONT]
[FONT=Andale Mono][~] $ teslacmd -a[/FONT]
[FONT=Andale Mono]Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD[/FONT]
[FONT=Andale Mono]'[{"color":null,"display_name":null,"id":187,"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","user_id":220,"vehicle_id":1234567890,"vin":"1A2B3CD5E6F7G8H9I","tokens":[],"state":"online"}]'[/FONT]
[FONT=Andale Mono][~] $ teslacmd -v[/FONT]
[FONT=Andale Mono]Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD[/FONT]
[FONT=Andale Mono]{ df: 0,[/FONT]
[FONT=Andale Mono]  dr: 0,[/FONT]
[FONT=Andale Mono]  pf: 0,[/FONT]
[FONT=Andale Mono]  pr: 0,[/FONT]
[FONT=Andale Mono]  ft: 0,[/FONT]
[FONT=Andale Mono]  rt: 0,[/FONT]
[FONT=Andale Mono]  car_version: '1.49.90',[/FONT]
[FONT=Andale Mono]  locked: true,[/FONT]
[FONT=Andale Mono]  sun_roof_installed: true,[/FONT]
[FONT=Andale Mono]  sun_roof_state: 'unknown',[/FONT]
[FONT=Andale Mono]  sun_roof_percent_open: 0,[/FONT]
[FONT=Andale Mono]  dark_rims: false,[/FONT]
[FONT=Andale Mono]  wheel_type: 'Silver21',[/FONT]
[FONT=Andale Mono]  has_spoiler: false,[/FONT]
[FONT=Andale Mono]  roof_color: 'None',[/FONT]
[FONT=Andale Mono]  perf_config: 'Base',[/FONT]
[FONT=Andale Mono]  exterior_color: 'Blue' }
[~] $ teslacmd -a[/FONT]
[FONT=Andale Mono]Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD[/FONT]
[FONT=Andale Mono]'[{"color":null,"display_name":null,"id":123,"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","user_id":123,"vehicle_id":1234567890,"vin":"1A2B3CD5E6F7G8H9I","tokens":["12345678a9bc0123","45678901d2ef3456"],"state":"online"}]'[/FONT]
 
Last edited:
Way back when there was a shell script posted which uses curl to access the car.
I have (had) a cron job which texted me if the car wasn't charging at certain times
(I frequently forget to plug in when I get home). When sleep mode was introduced
this script stopped working and I started playing around with it (looping instead of
failing, etc). But time constraints have kept me from perfecting it.

Does anyone have an updated curl-based shell script which has the appropriate
handling of a sleeping car? Is there a better solution out there?

Thanks,
/Ed

I think that was my script. I've now put a wake_up and sleep 60 before the charge_state and that makes it work again.
 
I think that was my script. I've now put a wake_up and sleep 60 before the charge_state and that makes it work again.

Can you post the code or point me to it if you don't mind.

I've been trying to get the "teslams" stuff installed on my server but alas it runs BSD and I need some other packages which I haven't been able to installed cleanly.
(This would be a moot point if I had root access...)
 
@hans and @ACDriveMotor
Thanks for checking this out. Despite your information I still cannot get this to work any more :confused:
Maybe the car's network connection has a problem (only one bar showing) so I will try again during the weekend after connecting it to my home WiFi.