@@ -88,38 +88,14 @@ public function getExistingTokens()
8888 unset($ tokens ['access_token ' ]);
8989 }
9090 $ this ->client ->setTokensFromStore ($ tokens );
91- $ this ->logger ->debug ('Using existing tokens ' );
92- }
93- return true ;
94- }
95-
96- /**
97- * Authentication with Netatmo server (OAuth2)
98- *
99- * @param string $username Netatmo account username
100- * @param string $password Netatmo account password
101- *
102- * @return Authentication result
103- */
104- public function getToken ($ username , $ password )
105- {
106- if (!$ this ->isMocked ) {
107- try {
108- $ this ->logger ->debug ('Request token with provided username and password ' );
109- $ this ->client ->setVariable ('username ' , $ username );
110- $ this ->client ->setVariable ('password ' , $ password );
111- $ tokens = $ this ->client ->getAccessToken ();
112- } catch (Netatmo \Exceptions \NAClientException $ e ) {
113- $ this ->logger ->error ('An error occured while trying to get tokens, check your username and password ' );
114- $ this ->logger ->debug ('Reason: ' .$ e ->getMessage ());
115- return false ;
91+ $ newTokens = $ this ->client ->getAccessToken ();
92+ if (array_key_exists ('refresh_token ' , $ newTokens )) {
93+ $ this ->logger ->debug ('Refresh token updated ' );
94+ $ newTokens ['expires_at ' ] = time () + $ newTokens ['expires_in ' ] - 30 ;
95+ $ this ->logger ->trace ('Update tokens: ' .json_encode ($ newTokens ));
96+ file_put_contents ($ this ::TOKENS_FILE , json_encode ($ newTokens ));
11697 }
117- $ this ->logger ->debug ('Token received ' );
118- $ this ->logger ->trace ('Token: ' .json_encode ($ tokens ));
119- // Store tokens
120- $ tokens ['expires_at ' ] = time () + $ tokens ['expires_in ' ] - 30 ;
121- $ this ->logger ->debug ('Access token expires at ' . $ tokens ['expires_at ' ]);
122- file_put_contents ($ this ::TOKENS_FILE , json_encode ($ tokens ));
98+ $ this ->logger ->debug ('Using existing tokens ' );
12399 }
124100 return true ;
125101 }
0 commit comments