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

MichaelR.

New Member
Jan 14, 2020
2
0
Germany
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
 

mjhwa

Member
May 20, 2021
21
20
CA
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);
 

Products we're discussing on TMC...

About Us

Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.

Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.


SUPPORT TMC
Top