Skip to content

Using the refresh token timeout ['expires_in'] updated 14-nov-2024 #20

@Cadkey

Description

@Cadkey

@KiboOst, I made a small modification to your API to use the expiration time (3 hours currently) of the refresh token ['expires_in']
This allows for fewer authentication calls and less disk writing on my NAS.
To be integrated into the API (or not) for those who are interested.
Thanks for all your php.

Update: I corrected an error by copying my changes.
The refreshtoken.json file is in this form: {"access_token":"xxxxxx","refresh_token":"yyyyyy"}
with your tokens in xxxxxx and yyyyyy
"expires_at" is generated in the json file by the API

protected $tokenFilePath = 'refreshtoken.json';

public function connect()
{
	$token_url = $this->_apiurl.'/oauth2/token';
	$postdata = http_build_query(
								array(
									'grant_type' => 'refresh_token',
									'client_id' => $this->_Netatmo_app_id,
									'client_secret' => $this->_Netatmo_app_secret,
									'refresh_token' => $this->_refresh_token
			)
		);
	$opts = array('http' =>
						array(
							'method'  => 'POST',
							'header'  => 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'."\r\n".
										'User-Agent: netatmoclient',
							'content' => $postdata
			)
		);
	$context  = stream_context_create($opts);

	$response = @file_get_contents($token_url, false, $context);

	//netatmo server sometimes give 500, always works second time:
	if ($response === false) {
		$response = @file_get_contents($token_url, false, $context);
		if ($response === false) {
			$tokens = json_decode(file_get_contents($this->tokenFilePath), true);
			if (time() > $tokens['expires_at']) $this->error =  "Token is expired. Use token generator at dev.netatmo.com"
			else $this->error = "Can't connect to Netatmo Server.";
			return false;
		}
	}
	$jsonDatas = json_decode($response, true);
	if (isset($jsonDatas['refresh_token']))
	{
		$tokens = array('access_token' => $jsonDatas['access_token'], 'refresh_token' => $jsonDatas['refresh_token'], 'expires_at' => strtotime(date('d.m.Y H:i'))+$jsonDatas['expires_in']);
		file_put_contents($this->tokenFilePath, json_encode($tokens, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
		$this->_refreshtoken = $jsonDatas['refresh_token'];
		$this->_accesstoken = $jsonDatas['access_token'];
		return true;
	}
	else
	{
		$this->error = "Can't get Netatmo token.";
		return false;
	}
}

function __construct($Netatmo_app_id, $Netatmo_app_secret, $homeID=0)
{
	$this->_Netatmo_app_id = $Netatmo_app_id;
	$this->_Netatmo_app_secret = $Netatmo_app_secret;

	if (file_exists($this->tokenFilePath)) {
		$tokens = json_decode(file_get_contents($this->tokenFilePath), true);
		$this->_refresh_token = $tokens['refresh_token'];
		$this->_accesstoken = $tokens['access_token'];
	}
	else
	{
		$this->error = "No refreshtoken file.";
		return false;
	}

	$this->_homeID = $homeID;
	if (isset($tokens['expires_at']) && time()<$tokens['expires_at']) $var = true;
	else $var = $this->connect();
	if ($var == true)
	{
		$this->getWeatherDatas();
		$this->getCameras();
		$this->getPersons();
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions