17.07.2023, 13:40
- generate a refresh_token with the right scope in http://dev.netatmo.com under my apps and choose for Token generator
- Copy the Refresh Token to the variable refresh_token
Now every run of the function get_token refreshed the token and saved it for further use.
- Copy the Refresh Token to the variable refresh_token
Now every run of the function get_token refreshed the token and saved it for further use.
Code:
-- get auth token from refresh token
refresh_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
function get_token()
refresh = storage.get('netatmo_refresh_1', refresh_token)
local request_body = "grant_type=refresh_token&client_id=" .. client_id .."&client_secret=" .. client_secret .. "&refresh_token=" .. refresh
local body, code, hdrs, stat = 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
if (netatmo_debug) then
log("netatmo auth error: "..tostring(code)..", "..tostring(body))
end
return
end
local response_decode = json.decode(table.concat(response_body))
local access_token = response_decode.access_token
if response_decode.refresh_token then
storage.set('netatmo_refresh_1', response_decode.refresh_token)
end
if (netatmo_debug) then
log(response_body)
log("netatmo token %s",access_token)
end
return "access_token=" .. access_token
end