Posts: 451
Threads: 94
Joined: Jun 2015
Reputation:
6
(26.01.2022, 13:00) admin Wrote: Specify API_Token = 'xyz' together with other credentials then you should be able to use the RequestFromTesla/WriteToTesla functions. Keep in mind that the token has expiration time and has to be refreshed.
I received an 401 error if i do what you suggested.
API_Token is the Refresh Token?
But why the other credentials? they are only used in the GetToken function and never used if you hardcoded the api_token.
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
471
Refresh token is what you use to get a new access and refresh token pair. Access token is used for API calls. It might have already expired so you need to generate a new token pair.
You will need a scheduled script to refresh the token periodically. See this for more info:
https://tesla-api.timdorr.com/api-basics...cess-token
Posts: 451
Threads: 94
Joined: Jun 2015
Reputation:
6
(27.01.2022, 07:26) admin Wrote: Refresh token is what you use to get a new access and refresh token pair. Access token is used for API calls. It might have already expired so you need to generate a new token pair.
You will need a scheduled script to refresh the token periodically. See this for more info: https://tesla-api.timdorr.com/api-basics...cess-token
i changed the code a little bit, but always received a deny from the webservice
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
function GetToken ()
body =
json.encode ({
grant_type =
'authorization_code' ,
client_id =
'ownerapi' ,
refresh_token =
refreshToken ,
scope =
'openid email offline_access'
})
resp = {}
res ,
code ,
headers =
http.request ({
url =
'https://auth.tesla.com/oauth2/v3/token' ,
method =
'POST' ,
source =
ltn12.source.string (
body ),
sink =
ltn12.sink.table (
resp ),
headers = {
[
'Content-Type' ] =
'application/json' ,
[
'Accept' ] =
'application/json' ,
[
'Content-Length' ] = #
body
}
})
log (
res ,
code ,
headers )
end
As refreshToken i used the Refresh Token from the Auth for Tesla app.
When i hardcoded the Access Token in the code it works, but this code expires in 8 hours
Code:
1 2 3
function GetToken ()
return accessToken
end
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
471
Try adding User-Agent/X-Tesla-User-Agent headers as in previous examples. Also try adding client_secret field to the JSON body. You might also need an additional request to exchange bearer token for access token.
Posts: 451
Threads: 94
Joined: Jun 2015
Reputation:
6
(27.01.2022, 09:41) admin Wrote: Try adding User-Agent/X-Tesla-User-Agent headers as in previous examples. Also try adding client_secret field to the JSON body. You might also need an additional request to exchange bearer token for access token.
I already tried that, but same 400 deny error
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
function GetToken ()
body =
json.encode ({
grant_type =
'authorization_code' ,
client_id =
'ownerapi' ,
client_secret =
client_secret ,
refresh_token =
refreshToken ,
scope =
'openid email offline_access'
})
resp = {}
res ,
code ,
headers =
http.request ({
url =
'https://auth.tesla.com/oauth2/v3/token' ,
method =
'POST' ,
source =
ltn12.source.string (
body ),
sink =
ltn12.sink.table (
resp ),
headers = {
[
'Content-Type' ] =
'application/json' ,
[
'Accept' ] =
'application/json' ,
[
'Content-Length' ] = #
body ,
[
'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'
}
})
log (
res ,
code ,
headers )
end
Posts: 451
Threads: 94
Joined: Jun 2015
Reputation:
6
problem found, grant_type incorrect transfered
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
function GetToken ()
body =
json.encode ({
grant_type =
'refresh_token' ,
client_id =
'ownerapi' ,
refresh_token =
refreshToken ,
scope =
'openid email offline_access'
})
resp = {}
res ,
code ,
headers =
http.request ({
url =
'https://auth.tesla.com/oauth2/v3/token' ,
method =
'POST' ,
source =
ltn12.source.string (
body ),
sink =
ltn12.sink.table (
resp ),
headers = {
[
'Content-Type' ] =
'application/json' ,
[
'Accept' ] =
'application/json' ,
[
'Content-Length' ] = #
body
}
})
if not res or code ~=
200 then
return
end
resp =
table.concat (
resp )
resp =
json.pdecode (
resp )
bearer_token =
resp.access_token
return bearer_token
end
And next? do i need to refresh some tokens?
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
471
When you get new bearer token you need to exchange it for an access token as described here:
https://tesla-api.timdorr.com/api-basics...cess-token
Your scheduled refresh script should work like this:
1. get current refresh_token from storage (you will have to store it manually once)
2. request new bearer_token and refresh_token using current refresh_token
3. exchange bearer_token for new access_token
4. save new access_token and new refresh_token to storage
All other API calls should use access_token from storage.
Posts: 3
Threads: 0
Joined: May 2020
Reputation:
0
this is great in that we seemed to have got to a solution but in the meantime, I have become confused. the most recent post explains how to obtain and maintain access tokens. Can someone provide a working example of the LUA code to set the backup reserve % in my battery and the mode (self-powered vs time-based control)? I'd really like to access the State of Charge and other parameters too if that is possible. My knowledge is scant but I am good at trial and error! with thanks
Posts: 179
Threads: 43
Joined: Jul 2015
Reputation:
2
Has anybody got working scripts for Tesla? Both to get token and to use API calls and want to share?
Posts: 10
Threads: 2
Joined: Sep 2016
Reputation:
0
20.12.2022, 21:50
(This post was last modified: 20.12.2022, 21:56 by eilert .)
Hi, I finally got these working by following the different advices in this thread and some googling. I especially wanted to control the rate of charge and charging limit. After some fiddling, I found out that these needs the arguments in the body.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
encdec =
require (
'encdec' )
http =
require (
'socket.http' )
mime =
require (
'mime' )
ltn12 =
require (
'ltn12' )
json =
require (
'json' )
https =
require (
'ssl.https' )
username =
'some@email.com'
password =
'somepwd'
client_id =
"replace"
client_secret =
"replace"
tesla = {}
tesla [
1 ] = {
'myCar1' ,
'id_s' }
CarID =
tesla [
1 ][
2 ]
RefreshToken_App =
"ey...XYZ"
AccessToken_App =
"ey...123"
function GetToken ()
RefreshToken =
storage.get (
'TeslaRefresh' )
body =
json.encode ({
grant_type =
'refresh_token' ,
client_id =
'ownerapi' ,
refresh_token =
RefreshToken ,
scope =
'openid email offline_access'
})
resp = {}
res ,
code ,
headers =
http.request ({
url =
'https://auth.tesla.com/oauth2/v3/token' ,
method =
'POST' ,
source =
ltn12.source.string (
body ),
sink =
ltn12.sink.table (
resp ),
headers = {
[
'Content-Type' ] =
'application/json' ,
[
'Accept' ] =
'application/json' ,
[
'Content-Length' ] = #
body ,
[
'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'
}
})
if not res or code ~=
200 then
log (
'Failed to Tesla API tokens' ,
resp ,
res ,
code )
return
end
resp =
table.concat (
resp )
resp =
json.pdecode (
resp )
storage.set (
'TeslaAccess' ,
resp.access_token )
storage.set (
'TeslaRefresh' ,
resp.refresh_token )
log (
'Tesla tokens successfully updated' )
log (
'New access token: ' ..
resp.access_token )
log (
'New refresh token: ' ..
resp.refresh_token )
end
function RequestFromTesla (
request )
API_Token =
storage.get (
'TeslaAccess' )
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
GetToken ()
log (
'Request from Tesla API failed ' ,
code ,
ret )
end
end
function WriteToTesla (
request ,
request_body )
API_Token =
storage.get (
'TeslaAccess' )
request_url =
'https://owner-api.teslamotors.com/api/1/' ..
request
local response_body = {}
local content_length
if type (
request_body ) ==
"string" then
content_length = #
request_body
else
request_body =
""
end
local body ,
code ,
hdrs ,
stat =
ssl.https.request {
url =
request_url ;
method =
"POST" ;
headers =
{
[
"Content-Type" ] =
"application/json" ;
[
"Authorization" ] =
"Bearer " ..
API_Token ;
[
"Content-Length" ] =
content_length ;
};
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
log (
'Write to Tesla API failed ' ,
code ,
ret )
GetToken ()
end
end
function get_products ()
products =
RequestFromTesla (
'products' )
return (
products )
end
function get_vehicles ()
vehicles =
RequestFromTesla (
'vehicles' )
return (
vehicles )
end
function get_onboarding_data ()
onboarding_data =
RequestFromTesla (
'users/onboarding_data' )
return (
onboarding_data )
end
function get_vehicle_data ()
vehicle_data =
RequestFromTesla (
'vehicles/' ..
CarID ..
'/vehicle_data' )
return (
vehicle_data )
end
function get_charge_state ()
charge_state =
RequestFromTesla (
'vehicles/' ..
CarID ..
'/data_request/charge_state' )
return (
charge_state )
end
function get_vehicle_state ()
vehicle_state =
RequestFromTesla (
'vehicles/' ..
CarID ..
'/data_request/vehicle_state' )
return (
vehicle_state )
end
function get_drive_state ()
drive_state =
RequestFromTesla (
'vehicles/' ..
CarID ..
'/data_request/drive_state' )
return (
drive_state )
end
function wake_up ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/wake_up' )
debugLog (
result )
end
function door_unlock ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/door_unlock' )
return (
result )
end
function door_lock ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/door_lock' )
return (
result )
end
function honk_horn ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/honk_horn' )
return (
result )
end
function flash_lights ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/flash_lights' )
return (
result )
end
function auto_conditioning_start ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/auto_conditioning_start' )
return (
result )
end
function auto_conditioning_stop ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/auto_conditioning_stop' )
return (
result )
end
function set_temps (
iDriverTemp ,
iPassengerTemp )
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/set_temps?driver_temp=' ..
iDriverTemp.. '&passenger_temp=' ..
iPassengerTemp )
return (
result )
end
function charge_port_door_open ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/charge_port_door_open' )
return (
result )
end
function set_charging_current (
iChargingAmps )
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/set_charging_amps' ,
'{"charging_amps":"' ..
iChargingAmps.. '"}' )
return (
result )
end
function set_charge_limit (
iChargingLimit )
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/set_charge_limit' ,
'{"percent":"' ..
iChargingLimit.. '"}' )
return (
result )
end
function charge_start ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/charge_start' )
return (
result )
end
function charge_stop ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/charge_stop' )
return (
result )
end
function charge_standard ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/charge_standard' )
return (
result )
end
function charge_standard ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/charge_max_range' )
return (
result )
end
function actuate_trunk ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/actuate_trunk' ,
'{"which_trunk":"rear"}' )
return (
result )
end
function actuate_frunk ()
result =
WriteToTesla (
'vehicles/' ..
CarID ..
'/command/actuate_trunk' ,
'{"which_trunk":"front"}' )
return (
result )
end