This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Simple HTTP - Tesla API
#7
Hi,

In terms of the Token... this only last for 45 days. The script you made, will this ask for a new token every time it is run?

For some commands they require and extra "field/input"...
Like the Start Drive Command, this one requires a password as well
https://www.teslaapi.io/vehicles/commands

How do I incorporate this into the script?

I also made som changes to the script, for anyone that plans to do this, I can confirm this works:
PS: you must run the script once, so you can get your vehicle ID. Look in the logs and replace the 123 for CarID

Code:
-- Load modules
require('json')
require('ssl.https')
require('ltn12')

-- Set credentials for Tesla API
username = "abc" -- your email for tesla account
password = "123" -- your password for tesla account
password1 = "password" -- default do not change
client_id = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384" -- default, do not change unless Tesla makes changes
client_secret = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3" -- default, do not change unless Tesla makes changes

function GetToken()
 request_token_url = 'https://owner-api.teslamotors.com/oauth/token'
 local response_body = {}
 local request_body = "grant_type=" .. password1 .. "&client_id=" .. client_id .. "&client_secret=" .. client_secret .. "&email=" .. username .. "&password=" .. password
 local body, code, hdrs, stat = ssl.https.request{
   url = request_token_url;
   method = "POST";
   headers =
   {
     ["Content-Type"] = "application/x-www-form-urlencoded";
     ["Content-Length"] = #request_body;
   };
   source = ltn12.source.string(request_body);
   sink = ltn12.sink.table(response_body);
 }
 if code == 200 then
   ret = table.concat(response_body)
   ret = json.pdecode(ret)
   return ret.access_token
 end
end

-- Request token
if not API_Token then
    API_Token = GetToken()
end

function RequestFromTesla(request)
 request_url = 'https://owner-api.teslamotors.com/api/1/' .. request
 local response_body = {}
 local request_body = ""
 local body, code, hdrs, stat = ssl.https.request{
   url = request_url;
   method = "GET";
   headers =
   {
     ["Content-Type"] = "application/json";
     ["Authorization"] = "Bearer " .. API_Token;
   };
   source = ltn12.source.string(request_body);
   sink = ltn12.sink.table(response_body);
 }
 if code == 200 then
   ret = table.concat(response_body)
   ret = json.pdecode(ret)
   return ret
 else
   API_Token = GetToken() -- request a new token
 end
end

function WriteToTesla(request)
  request_url = 'https://owner-api.teslamotors.com/api/1/' .. request
  local response_body = {}
  local request_body = ""
  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else 
    API_Token = GetToken() -- request a new token
  end
end

-- Get products
products = RequestFromTesla('products')
log(products) -- Check here your Car ID

CarID = 123 -- run the script once, and look in the logs you will find a string called id_s this number is your vehicle ID, this should be replaced with the 123 in this field, remember to add "", then run script again, and you will get it to work!

-- Get onboarding data
onboarding_data = RequestFromTesla('users/onboarding_data')
log(onboarding_data)

-- unlock car
result = WriteToTesla('vehicles/' .. CarID .. '/command/door_unlock')
log(result)
BR,
Mr.D
Reply


Messages In This Thread
Simple HTTP - Tesla API - by Mr.D - 13.12.2018, 19:35
RE: Simple HTTP - Tesla API - by Mr.D - 13.12.2018, 19:59
RE: Simple HTTP - Tesla API - by Mr.D - 14.12.2018, 15:19
RE: Simple HTTP - Tesla API - by Mr.D - 27.01.2019, 12:04
RE: Simple HTTP - Tesla API - by admin - 10.01.2020, 09:21
RE: Simple HTTP - Tesla API - by alexgleyzer - 26.05.2020, 07:38
RE: Simple HTTP - Tesla API - by admin - 26.05.2020, 08:19
RE: Simple HTTP - Tesla API - by alexgleyzer - 26.05.2020, 10:35
RE: Simple HTTP - Tesla API - by admin - 26.05.2020, 11:14
RE: Simple HTTP - Tesla API - by alexgleyzer - 27.05.2020, 06:34

Forum Jump: