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

Tesla API and Google Script

This site may earn commission on affiliate links.
Hello everybody,
i try to access the Tesla API via Google Script.

Getting a token via Google Script works without any problem (i've tried the token with another program).
But as soon as i try to access the url "https://owner-api.teslamotors.com/api/1/vehicles" for example, i get an 401 error.

Here is the code (i know it's not good code, but i'm still starting with this ;) )

Code:
function access_token(token) {
  var url = "https://owner-api.teslamotors.com/oauth/token";
 
  var data = {
    'grant_type': 'password',
    'client_id': '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
    'client_secret': 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3',
    'email': '<email>',
    'password': '<password>'
  };
 
  var headers = {
    "contentType": "application/json",
    'payload' : JSON.stringify(data)
  };
 
  var response = UrlFetchApp.fetch(url, headers);
  var access_token = JSON.parse(response.getContentText())
  return access_token["access_token"]
}

function list(){
  var headers = {
    "User-Agent": "Mozilla/5.0 (Linux; Android 9.0.0; VS985 4G Build/LRX21Y; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.83 Mobile Safari/537.36",
    "X-Tesla-User-Agent": "TeslaApp/3.4.4-350/fad4a582e/android/9.0.0",
    "contentType": "application/json",
    "authorization": "Bearer " + access_token(),
  }
 
  var url = "https://owner-api.teslamotors.com/api/1/vehicles"
 
  var carlist = UrlFetchApp.fetch(url, headers)
  Logger.log(carlist)
}

I want to try to add the car data directly into a google sheet.

Hopefully somebody of you can help me :)

Greetings,
Michael
 
Hello everybody,
i try to access the Tesla API via Google Script.

Getting a token via Google Script works without any problem (i've tried the token with another program).
But as soon as i try to access the url "https://owner-api.teslamotors.com/api/1/vehicles" for example, i get an 401 error.

Here is the code (i know it's not good code, but i'm still starting with this ;) )

Code:
function access_token(token) {
  var url = "https://owner-api.teslamotors.com/oauth/token";
 
  var data = {
    'grant_type': 'password',
    'client_id': '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
    'client_secret': 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3',
    'email': '<email>',
    'password': '<password>'
  };
 
  var headers = {
    "contentType": "application/json",
    'payload' : JSON.stringify(data)
  };
 
  var response = UrlFetchApp.fetch(url, headers);
  var access_token = JSON.parse(response.getContentText())
  return access_token["access_token"]
}

function list(){
  var headers = {
    "User-Agent": "Mozilla/5.0 (Linux; Android 9.0.0; VS985 4G Build/LRX21Y; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.83 Mobile Safari/537.36",
    "X-Tesla-User-Agent": "TeslaApp/3.4.4-350/fad4a582e/android/9.0.0",
    "contentType": "application/json",
    "authorization": "Bearer " + access_token(),
  }
 
  var url = "https://owner-api.teslamotors.com/api/1/vehicles"
 
  var carlist = UrlFetchApp.fetch(url, headers)
  Logger.log(carlist)
}

I want to try to add the car data directly into a google sheet.

Hopefully somebody of you can help me :)

Greetings,
Michael
I think your syntax is wrong. Try:

var options = {
'headers': {insert your headers here}
};

var url = 'https://owner-api.teslamotors.com/api/1/vehicles';

var carlist = UrlFetchApp.fetch(url, options);