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
#5
(13.12.2018, 22:50)Erwin van der Zwart Wrote: Hi,

Usually the bearer token is a response on the token request and is valid for x minutes (mostly 60 minutes), so i doubt that 123 is the bearer code (: 

Can you try this?
Code:
-- Load modules require('json') require('ssl.https') require('ltn12') -- Set credentials for Tesla API username = "admin" password = "admin" function GetToken()  request_token_url = 'https://owner-api.teslamotors.com/oauth/token'  local response_body = {}  local request_body = "grant_type=password&username=" .. 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 -- could be some other field, try to log ret to be sure  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 = 1 -- products[1].id? -- Get onboarding data onboarding_data = RequestFromTesla('users/onboarding_data') log(onboarding_data) -- Wake up car result = WriteToTesla('vehicles/' .. CarID .. '/wake_up') log(result)
BR,

Erwin

Hi,

If I understand your script correctly this will get me the bearer token? and then some?
I already have the token, and you are correct, it is not 123. I just wrote that because I did not want to expose the token. Doing so, the token together with the Vehicle ID/Car ID I would give everyone access to my car Smile
In terms of how long the token last, a Tesla token lasts for 30 days before it needs to be refreshed.

I already have the Vehicle ID, which is part of the URL.
What I need is to understand how I send a POST request that includes the URL but also the bearer token... I do not know how to add the bearer token as part of my URL request...


But I see your script is way more advanced, in fear of asking too much could you explain what your script does? in terms of the different functions? I see that storing and using the response from the car adds some further potentials in terms of adding "features" to my smarthouse/tesla Smile
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: