-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetatmo_manage_tokens.php
622 lines (542 loc) · 22.1 KB
/
netatmo_manage_tokens.php
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/php
<?php
/*------------------------------------------------------------------------------*/
/* 2025/01/20 (c) Phil353556 github.com */
/* email: [email protected] */
/* */
/* This piece of software is as IS with no Warranty. Use it of your own risk! */
/* See LICENCE.md file in repository */
/*------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------*/
/* The goal is to manage tokens so it is possible then to interact with Netatmo devices */
/* Run the script without any parameter to display the help */
/*------------------------------------------------------------------------------*/
/* list of function used */
/* */
/*function f_read_file_parameters($display) */
/*function f_get_tokens($grant_type,$client_id,$client_secret,$code,$scope,$redirect_uri,$Content_Type)*/
/*function f_writefile($access_token_to_write,$refresh_token_to_write) */
/*function f_readfile() */
/*function f_get_refresh_tokens($grant_type,$refresh_token,$client_id,$client_secret)
/*function f_usage() */
/*------------------------------------------------------------------------------
Return codes by function:
function f_read_file_parameters($display)
10 : file parameters.txt do not exist
function f_get_tokens($grant_type,$client_id,$client_secret,$code,$scope,$redirect_uri,$Content_Type)
20 : the http code is not 200
function f_writefile($access_token_to_write,$refresh_token_to_write)
30 : file file_access_token.txt do not exist
31 : file file_access_token.txt cannot be written
32 : file file_refresh_token.txt do not exist
33 : file file_refresh_token.txt cannot be written
34 : file file_expire_time.txt do not exist
35 : file file_expire_time.txt cannot be written
function f_readfile()
40 : file file_access_token.txt do not exist
41 : file file_refresh_token.txt do not exist
42 : file file_expire_time.txt do not exist
function f_get_refresh_tokens($grant_type,$refresh_token,$client_id,$client_secret)
50 : file file_expire_time.txt do not exist
51 : file file_refresh_token.txt do not exist
52 : the http code is not 200
function f_usage()
-> no return code
Main routine
0 : normal run
100 : no parameter on command line
101 : usage was the first parameter
102 : display was the first parameter
103 : current was the first parameter
104 : refresh was the first parameter
105 : code length must be 32 caracters
106 : code must composed with letters and numbers only
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------*/
/* The lines hereunder SHOULD NOT be modified */
/*------------------------------------------------------------------------------*/
$file_parameters="file_parameters.txt";
$file_access_token="file_access_token.txt";
$file_refresh_token="file_refresh_token.txt";
$file_expire_time="file_expire_time.txt";
$Content_Type="application/x-www-form-urlencoded;charset=UTF-8";
$grant_type="authorization_code";
/*------------------------------------------------------------------------------*/
/* This function read the file with parameters needed and set variables */
/*------------------------------------------------------------------------------*/
function f_read_file_parameters($display)
{
global $DEBUG;
global $file_parameters;
global $client_id;
global $client_secret;
global $scope;
global $redirect_uri;
if ( $DEBUG == true )
{
printf(" --------------------------------------------------------------------------\n");
printf(" function: f_read_file_parameters\n");
}
if ( $display == "true" )
{
printf(" ----------------------------------------------------------------------------------- \n");
printf(" ".date('Y-m-d H:i:s\Z',time())." (UTC) \n");
printf(" \n");
}
if (file_exists($file_parameters) )
{
$file_parameters_txt = file_get_contents($file_parameters);
$rows = explode("\n", $file_parameters_txt);
foreach($rows as $row => $data)
{
$test=trim($data);
if ( !empty($test) )
{
$row_data = explode('=', $data);
$info[$row]['id'] = $row_data[0];
$info[$row]['name'] = $row_data[1];
if ( $display == "true" )
{
printf(" Line #".$row." ".$info[$row]['id']."=".$info[$row]['name']." \n");
}
}
}
$client_id=$info[0]['name'];
$client_secret=$info[1]['name'];
$scope=$info[2]['name'];
$redirect_uri=$info[3]['name'];
if ( $DEBUG == true)
{
var_dump($info);
}
}
else
{
printf(" function f_read_file_parameters: file ".$file_parameters." do not exist \n");
exit(10);
}
if ( $display == "true" )
{
printf(" ----------------------------------------------------------------------------------- \n");
}
}
/*------------------------------------------------------------------------------*/
/* https://dev.netatmo.com/apidocumentation/oauth#authorization-code */
/*------------------------------------------------------------------------------*/
/*
POST /oauth2/token HTTP/1.1
Host: api.netatmo.com
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
grant_type=authorization_code
client_id=[YOUR_APP_ID]
client_secret=[YOUR_CLIENT_SECRET]
code=[CODE_RECEIVED_FROM_USER]
redirect_uri=[YOUR_REDIRECT_URI]
scope=[SCOPE_SPACE_SEPARATED]
*/
/*------------------------------------------------------------------------------*/
function f_get_tokens($grant_type,$client_id,$client_secret,$code,$scope,$redirect_uri,$Content_Type)
{
global $DEBUG;
global $client_id;
global $client_secret;
global $scope;
global $redirect_uri;
if ( $DEBUG == true )
{
printf(" ----------------------------------------------------------------------------------- \n");
printf(" function: f_get_tokens\n");
printf(" ----------------------------------------------------------------------------------- \n");
}
$handle = curl_init();
$datas = array("grant_type"=>$grant_type,"client_id"=>$client_id,"client_secret"=>$client_secret,"code"=>$code,"scope"=>$scope,"redirect_uri"=>$redirect_uri,"Content-Type:" => $Content_Type);
if ( $DEBUG == true )
{
var_dump($datas);
}
curl_setopt($handle, CURLOPT_POSTFIELDS, $datas);
if ( $DEBUG == true )
{
curl_setopt_array($handle, array(
CURLOPT_URL => "https://api.netatmo.com/oauth2/token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $datas,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true
)
);
}
else
{
curl_setopt_array($handle, array(
CURLOPT_URL => "https://api.netatmo.com/oauth2/token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $datas,
CURLOPT_RETURNTRANSFER => true
)
);
}
$result=curl_exec($handle);
$httpcode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if($httpcode != 200 ) {
printf(" ----------------------------------------------------------------------------------- \n");
printf(" ERROR: serveur return code is: ".$httpcode." \n");
printf(" ----------------------------------------------------------------------------------- \n");
exit(20);
}
$array = json_decode($result, true);
if ( $DEBUG == true )
{
var_dump($array);
printf(" httpcode; ".$httpcode."\n");
printf(" access_token; ".$array['access_token']."\n");
printf(" refresh_token: ".$array['refresh_token']."\n");
printf(" access_token_expire_in: ".$array['expires_in']."\n");
}
printf(" ----------------------------------------------------------------------------------- \n");
return [$array['access_token'],$array['refresh_token'],$array['expires_in']];
}
/*------------------------------------------------------------------------------*/
/* */
/*------------------------------------------------------------------------------*/
function f_writefile($access_token_to_write,$refresh_token_to_write)
{
global $DEBUG;
global $file_access_token;
global $file_refresh_token;
global $file_expire_time;
if ( $DEBUG == true )
{
printf(" ----------------------------------------------------------------------------------- \n");
printf(" function: f_write files\n");
printf(" file : ".$file_access_token." \n");
printf(" file : ".$file_refresh_token." \n");
}
if (!$myfile = fopen($file_access_token, 'c')) {
printf(" The file cannot be open: $file_access_token \n");
exit(30);
}
if (fwrite($myfile, $access_token_to_write) === FALSE) {
printf(" Cannot write in the file: $file_access_token \n");
exit(31);
}
printf(" Write succesful $access_token_to_write in file $file_access_token \n");
fclose($myfile);
if (!$myfile = fopen($file_refresh_token, 'c')) {
printf(" The file cannot be open: $file_refresh_token \n");
exit(32);
}
if (fwrite($myfile, $refresh_token_to_write) === FALSE) {
printf( "Cannot write in the file: $file_refresh_token \n");
exit(33);
}
printf(" Write succesful $refresh_token_to_write in file $file_refresh_token \n");
fclose($myfile);
$current_unix_time=time();
if (!$myfile = fopen($file_expire_time, 'c')) {
printf(" The file cannot be open: $file_expire_time \n");
exit(34);
}
if (fwrite($myfile, $current_unix_time) === FALSE) {
printf(" Cannot write in the file: $file_expire_time \n");
exit(35);
}
printf(" Write succesful $current_unix_time in file $file_expire_time \n");
fclose($myfile);
printf(" ----------------------------------------------------------------------------------- \n");
}
/*-------------------------------------------------------------------------------*/
/* Read the three files: */
/* - file_access_token.txt, */
/* - file_refresh_token.txt */
/* - file_expire_time.txt */
/* Display their content */
/* Calculate the name of second between the initial expire time received and now */
/*-------------------------------------------------------------------------------*/
function f_readfile()
{
global $DEBUG;
global $file_access_token;
global $file_refresh_token;
global $file_expire_time;
printf(" ----------------------------------------------------------------------------------- \n");
if ( $DEBUG == true )
{
printf(" function: f_readfiles\n\n");
}
printf(" ".date('Y-m-d H:i:s\Z',time())." (UTC) \n");
printf(" \n");
printf(" File : ".$file_access_token." \n");
if (!$myfile = @fopen($file_access_token, 'r')) {
printf(" The file cannot be open: $file_access_token \n");
exit(40);
}
printf(" ".$str=str_replace("\n","",fread($myfile,filesize($file_access_token)))."\n\n");
fclose($myfile);
printf(" File : ".$file_refresh_token." \n");
if (!$myfile = @fopen($file_refresh_token, 'r')) {
printf(" The file cannot be open: $file_access_token \n");
exit(41);
}
printf(" ".$str=str_replace("\n","",fread($myfile,filesize($file_refresh_token)))."\n\n");
fclose($myfile);
printf(" File : ".$file_expire_time." \n");
if (!$myfile = @fopen($file_expire_time, 'r')) {
printf(" The file cannot be open: $file_access_token \n");
exit(42);
}
$expire_time=fread($myfile,filesize($file_expire_time));
printf(" Initial time ".str_replace("\n","",$expire_time)." or ".@gmdate('Y-m-d H:i:s\Z',$expire_time)." (UTC)\n\n");
$expire_time= (int)$expire_time;
$delta=time()-$expire_time;
printf(" Delta time with now ".$delta." / 10800 \n");
fclose($myfile);
printf(" ----------------------------------------------------------------------------------- \n");
}
/*------------------------------------------------------------------------------*/
/* Refresh the access_token AND refresh_token */
/* using the current refresh token, client_id and client_secret */
/* The grand_type must be refresh_token */
/*------------------------------------------------------------------------------
Source: https://dev.netatmo.com/apidocumentation/oauth#refreshing-a-token
Endpoint: https://api.netatmo.com/oauth2/token
Method: POST
Entry parameters
Name Required Description
grant_type yes refresh_token
refresh_token yes the refresh token retrieved while requesting an access_token
client_id yes your client id
client_secret yes your client secret
Return parameters
Name Description
access_token Access token for your user
expires_in Validity timelaps in seconds
refresh_token Use this token to get a new access_token once it has expired
and all the scopes
------------------------------------------------------------------------------------------ */
function f_get_refresh_tokens($grant_type,$refresh_token,$client_id,$client_secret)
{
global $DEBUG;
global $client_id;
global $client_secret;
global $scope;
global $redirect_uri;
global $refresh_token;
global $access_token;
global $expire_in;
global $file_access_token;
global $file_refresh_token;
global $file_expire_time;
printf(" ----------------------------------------------------------------------------------- \n");
if ( $DEBUG == true )
{
printf(" function: f_get_refresh_tokens\n");
}
printf(" ".date('Y-m-d H:i:s\Z',time())." (UTC) \n");
printf(" \n");
printf(" file : ".$file_expire_time." \n");
if (!$myfile = @fopen($file_expire_time, 'r')) {
printf(" The file cannot be open: $file_expire_time \n");
exit(50);
}
$expire_time=fread($myfile,filesize($file_expire_time));
printf(" Initial time ".$expire_time." \n");
$expire_time= (int)$expire_time;
$delta=time()-$expire_time;
printf(" Delta time with now ".$delta." / 10800 \n");
if ( $delta > "10800" )
{
printf(" Token expired - refresh token is needed\n\n");
}
fclose($myfile);
printf(" Retrieve refresh token store in file : ".$file_refresh_token." \n");
if (!$myfile = @fopen($file_refresh_token, 'r')) {
printf(" The file cannot be open: $file_refresh_token \n");
exit(51);
}
$refresh_token=$str=str_replace("\n","",fread($myfile,filesize($file_refresh_token)));
$grant_type="refresh_token";
printf(" \n");
printf(" Getting NEW access token AND refresh token, using current refresh token\n");
printf(" \n");
if ( $DEBUG == true )
{
printf(" grant type:".$grant_type."\n");
printf(" refresh_token: ".$refresh_token."\n");
printf(" client_id: ".$client_id."\n");
printf(" client_secret: ".$client_secret." \n");
}
$handle = curl_init();
$datas = array("grant_type"=>$grant_type,"refresh_token"=>$refresh_token,"client_id"=>$client_id,"client_secret"=>$client_secret);
if ( $DEBUG == true )
{
var_dump($datas);
}
curl_setopt($handle, CURLOPT_POSTFIELDS, $datas);
if ( $DEBUG == true )
{
curl_setopt_array($handle, array(
CURLOPT_URL => "https://api.netatmo.com/oauth2/token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $datas,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true
)
);
}
else
{
curl_setopt_array($handle, array(
CURLOPT_URL => "https://api.netatmo.com/oauth2/token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $datas,
CURLOPT_RETURNTRANSFER => true
)
);
}
$result=curl_exec($handle);
$httpcode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
$array = json_decode($result, true);
if($httpcode != 200 ) {
printf(" --------------------------------------\n");
printf(" ERROR: serveur return code is: ".$httpcode." \n");
printf(" --------------------------------------\n");
var_dump($array);
exit(52);
}
if ( $DEBUG == true )
{
var_dump($array);
printf(" httpcode; ".$httpcode."\n");
printf(" access_token; ".$array['access_token']."\n");
printf(" refresh_token: ".$array['refresh_token']."\n");
printf(" access_token_expire_in: ".$array['expires_in']."\n");
}
printf(" ----------------------------------------------------------------------------------- \n");
return [$array['access_token'],$array['refresh_token'],$array['expires_in']];
}
/*------------------------------------------------------------------------------*/
/* Display the help for this script */
/*------------------------------------------------------------------------------*/
function f_usage()
{
printf(" ".date('Y-m-d H:i:s\Z',time())." (UTC) \n");
printf(" ----------------------------------------------------------------------------------- \n");
printf(" Usage: [usage|code|current|refresh ] \n");
printf(" \n");
printf(" usage \n");
printf(" This help \n");
printf(" If the program is run with no parameter, usage is displayed \n");
printf(" \n");
printf(" display \n");
printf(" Display the contents of the parameters file, there are four lines: \n");
printf(" -client_id \n");
printf(" -client_secret \n");
printf(" -scope \n");
printf(" -redirect-uri \n");
printf(" \n");
printf(" code \n");
printf(" Use the code (32c) displayed in URL bar \n");
printf(" AFTER the use of the specific URL similar to: \n");
printf(" https://api.netatmo.com/oauth2/authorize?client_id=xxxxxx \n");
printf(" \n");
printf(" If a valide code is the first parameter of the script then : \n");
printf(" - The access_token and refresh_token are requested from netatmo \n");
printf(" - The access token and refresh token are displayed \n");
printf(" - And store in two files: file_access_token.txt and file_refresh_token.txt \n");
printf(" - A third file file_expire_time.txt is also created \n");
printf(" \n");
printf(" current \n");
printf(" Display the contents of access token, refresh token and expire time files \n");
printf(" Calculate and display the number of seconds since the expire time obtained \n");
printf(" \n");
printf(" refresh \n");
printf(" Get a new access token and a new refresh token \n");
printf(" using the current refresh token \n");
printf(" ----------------------------------------------------------------------------------- \n");
}
/*------------------------------------------------------------------------------*/
/* Main Routine */
/*------------------------------------------------------------------------------*/
global $DEBUG;
global $client_id;
global $client_secret;
global $scope;
global $redirect_uri;
global $refresh_token;
global $access_token;
global $expire_time;
$env = getenv('NA_DEBUG');
if ( ( $env == 0 ) || $env == "false" )
{
$DEBUG = 0;
}
if ( ( $env == 1 ) || $env == "true" )
{
$DEBUG = 1;
printf(" ----------------------------------------------------------------------------------- \n");
printf(" DEBUG is ON \n");
printf(" ----------------------------------------------------------------------------------- \n");
}
$argc = count($argv);
if ( $argc != 2 ) { f_usage(); exit(100); }
if ( $DEBUG == true )
{
var_dump($argv);
}
if ( $argv[1] == "usage" )
{
f_usage();
exit(101);
}
if ( $argv[1] == "display" )
{
f_read_file_parameters("true");
exit(102);
}
if ( $argv[1] == "current" )
{
f_read_file_parameters("false");
f_readfile();
exit(103);
}
if ( $argv[1] == "refresh" )
{
f_read_file_parameters("false");
[$access_token, $refresh_token, $expire_token ] = f_get_refresh_tokens($grant_type,$refresh_token,$client_id,$client_secret);
f_writefile($access_token,$refresh_token);
exit(104);
}
else
{
$code = $argv[1];
if ( strlen($code) <> 32 )
{
printf(" The code must have 32c \n");
f_usage();
exit(105);
}
$result=ctype_alnum($code);
if (! $result)
{
printf(" KO ".$result." \n");
f_usage();
exit(106);
}
else
{
f_read_file_parameters("false");
printf(" ----------------------------------------------------------------------------------- \n");
printf(" ".date('Y-m-d H:i:s\Z',time())." (UTC) \n");
printf(" \n");
printf(" CODE = ".$code." \n");
[$access_token, $refresh_token, $expire_token ] = f_get_tokens($grant_type,$client_id,$client_secret,$code,$scope,$redirect_uri,$Content_Type);
printf(" atoken ".$access_token."\n rtoken ".$refresh_token."\n etoken ".$expire_token."\n");
f_writefile($access_token,$refresh_token);
}
}
exit(0)
?>
/* ------------------------------------------------------------------------------ */