Getting a 404 with NodeJS Request to Tesla API

802 views Asked by At

I am trying to retrieve my vehicle's data from the public Tesla API (https://www.teslaapi.io/vehicles/state-and-settings). I'm using NodeJS, with the Request and Express packages, I'm able to retrieve the initial accessToken, as well as the Vehicle ID number. However, when I'm trying to use these two values to actually make a GET request for the data, I keep getting a 404 error. I've been debugging for hours and am honestly not sure why. I hit the same endpoint in postman and get the correct response, any help would be greatly appreciated! Also, how can I avoid having to make subsequent requests inside the previous callback, would Async be the way to go? Thanks!

const express = require("express");
const app = express();
const port = 3000
var path = require("path");


var bodyParser = require("body-parser");

app.use(express.static(path.join(__dirname, '/public')));

app.set("view engine", "ejs");


var accessToken;

var request = require('request').defaults({
    headers: {
        "x-tesla-user-agent": "TeslaApp/3.4.4-350/fad4a582e/android/8.1.0",
        "user-agent": "Mozilla/5.0 (Linux; Android 8.1.0; Pixel XL Build/OPM4.171019.021.D1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36"}
    });




app.get("/", function(req, resp){

   /* GET ACCESS TOKEN */
     request({
       url: 'https://owner-api.teslamotors.com/oauth/token',
       method: 'POST',
       json: true,
       gzip: true,
       body: {
         "grant_type": 'password',
         "client_id": '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
         "client_secret":'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3',
         "email": 'MYUSERNAME',
         "password": 'MYPASSWORD'
       },
      }, function(err, res, body) {
       accessToken = body.access_token;

            /* GET THE VEHICLE ID NUMBER */
            request({
            url: 'https://owner-api.teslamotors.com/api/1/vehicles',
            method: 'GET',
            headers: { "Authorization": "Bearer " + accessToken, "Content-Type": "application/json; charset=utf-8" } 

           }, function(err, res, body) {
        parsedBody = JSON.parse(body);
        var vehicleID = parsedBody.response[0].id;
        resp.render('index', {hi: accessToken} );
        var vehicleIDString = vehicleID.toString();
        var finalURL = 'https://owner-api.teslamotors.com/api/1/vehicles/' + vehicleIDString + '/vehicle_data';


            /*  Get Vehicle Data    (THIS IS GIVING 404) */
            request({

                method: 'GET',
                url: 'https://owner-api.teslamotors.com/api/1/vehicles/' + vehicleIDString + '/vehicle_data',
                        headers: { "Authorization": "Bearer " + accessToken, "Content-Type": "application/json; charset=utf-8" } 

            }, 
                function(err, res, body){    
                console.trace();

            });//END GET VEHICLE DATA

      });//END VEHICLE ID NUMBER REQUEST


     });//END ACCESS TOKEN REQUEST

});




/* HIT ANY ENDPOINT WE WANT */







app.listen(port, () => {console.log('Example app listening on port ${port}!') } )
2

There are 2 answers

0
jav040 On

After stumbling onto some other thread, I realized that the original documentation was incorrect, and instead of id, it should've been id_s. So both the fault of subpar documentation, as well as my poor attention to the details. Thanks!!

https://github.com/timdorr/tesla-api/issues/53

1
Philippe Gressé-Lugué On

Today I had a similar error (404) with TeslaJS in NodeRed and it was due to Tesla having changed the vehicleID value (id_s field).

The "vehicles" command is still working then it is possible to retrieve the new id_s values and pass it to the following commands. When using this new id_s, the error disappears and the problem is solved.

Hope this can help some other peoples with same problem...

A good practice would probably to select id_s using VIN value which will not change over the time...