Skip to content

Commit b4687e8

Browse files
committed
fix(auth): Persist refresh token after used
Close #2 Remove deprecated Client credentials grant type to obtain token Fix versions in docker-compose
1 parent 8c46a74 commit b4687e8

File tree

6 files changed

+26
-51
lines changed

6 files changed

+26
-51
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
vendor
2+
mock/
3+
netatmo-collect.log
4+
tokens.json

Netatmo.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ Netatmo collector is a PHP script for requesting measures from Netatmo devices.
2121

2222
Download this project and extract files to directory of your choice.
2323

24-
Configure a Netatmo [application](https://dev.netatmo.com/myaccount/createanapp) and user account informations in `config.php`.
24+
In order to authenticate yourself on the Netatmo API, you must:
25+
26+
- create a Netatmo [application](https://dev.netatmo.com/myaccount/createanapp),
27+
- generate a token with `read_station` scope,
28+
- enter your `client_id` and `client_secret` values in `config.php`,
29+
- create a `tokens.json` file with this content (replace the 2 values between `<>` with your tokens):
30+
31+
``` json
32+
{"access_token":"<yourAccessToken>","refresh_token":"<yourRefreshToken >","scope":["read_station"],"expires_in":10800,"expire_in":10800,"expires_at":0}
33+
```
2534

2635
### Dependencies
2736

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nioc/netatmo-collector",
33
"description": "Netatmo collector is a script for requesting measures from Netatmo devices",
44
"homepage": "https://github.com/nioc/netatmo-collector",
5-
"version": "0.2.0",
5+
"version": "0.4.0",
66
"license": "AGPL-3.0-only",
77
"require": {
88
"influxdb/influxdb-php": "^1.14",

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ version: '3.2'
22

33
services:
44
php:
5-
image: php:alpine
5+
image: php:7.4-alpine
66
working_dir: /usr/src/app
7-
command: php index.php 2020-11-19
7+
command: php index.php 2023-12-05
88
depends_on:
99
- influxdb
1010
volumes:
@@ -17,7 +17,7 @@ services:
1717
- .:/app
1818

1919
influxdb:
20-
image: influxdb:alpine
20+
image: influxdb:1.8-alpine
2121
restart: unless-stopped
2222
ports:
2323
- "8086:8086"

index.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,8 @@
3333

3434
// Authentication with Netatmo server (OAuth2)
3535
if (!$netatmo->getExistingTokens()) {
36-
$logger->warn('No existing token found, try to get it with username and password');
37-
// No existing token found, connect with username and password read from stdin
38-
function prompt($question)
39-
{
40-
echo "\r\n$question: ";
41-
$stdin = fopen('php://stdin', 'r');
42-
$response = fgets($stdin);
43-
fclose($stdin);
44-
return trim($response);
45-
}
46-
$username = prompt('Your netatmo account username');
47-
$password = prompt('Your netatmo account password');
48-
if (!$netatmo->getToken($username, $password)) {
49-
exit(1);
50-
}
36+
$logger->error('No existing token found, go to https://dev.netatmo.com/apps to get a token');
37+
exit(1);
5138
}
5239

5340
// Retrieve user's Weather Stations Information

0 commit comments

Comments
 (0)