Hallo Fons,
Ich habe mir die Token-Abrufe mal angeschaut:
TXT: 27.02.2018 22:24:59.00 | BMW | token expired, get new token
TXT: 27.02.2018 22:24:59.00 | BMW Postfields | username=...
TXT: 27.02.2018 22:25:00.00 | BMW | access_token: K7kXly5HTO9Tdhy7xi1I7hKEvL8qyBl5
TXT: 27.02.2018 22:25:00.00 | BMW | Bearer expires in: 7199s
TXT: 27.02.2018 22:25:00.00 | BMW | set access_token: K7kXly5HTO9Tdhy7xi1I7hKEvL8qyBl5
TXT: 27.02.2018 22:25:00.00 | BMW | set token expiration: 1519773899
=> gueltig bis 00:24:59
TXT: 27.02.2018 22:25:34.00 | BMW | Send to url: https://www.bmw-connecteddrive.de/api/vehicle/dynamic/v1/WBAWY51xxxxxxxxxx?offset=-60
TXT: 27.02.2018 22:25:34.00 | BMW | 'Content-Type: application/json' , 'Authorization: Bearer ' K7kXly5HTO9Tdhy7xi1I7hKEvL8qyBl5
TXT: 27.02.2018 22:25:36.00 | BMW Response | {<LF> "attributesMap" : {<LF> "updateTime_converted" : "27.02.2018 17:35",<LF> "DCS_CCH_Ongoing " : null,<LF> ....
letzte Nachricht, die noch ok war
TXT: 28.02.2018 00:18:00.00 | BMW | Send to url: https://www.bmw-connecteddrive.de/api/vehicle/remoteservices/v1/WBAWY51xxxxxxxxxx/history
TXT: 28.02.2018 00:18:00.00 | BMW | 'Content-Type: application/json' , 'Authorization: Bearer ' K7kXly5HTO9Tdhy7xi1I7hKEvL8qyBl5
TXT: 28.02.2018 00:18:01.00 | BMW Response | [ {<LF> "eventId" : "304533343934322223178200@bmw.de",<LF> "creationTime" : 1519409922279,<LF> "status" : "SUCCESS" ...
ab hier Schrott. token ist abgelaufen, kein neuer wurde Token abgerufen
TXT: 28.02.2018 00:28:01.00 | BMW | Send to url: https://www.bmw-connecteddrive.de/api/vehicle/dynamic/v1/WBAWY51xxxxxxxxxx?offset=-60
TXT: 28.02.2018 00:28:01.00 | BMW | 'Content-Type: application/json' , 'Authorization: Bearer ' K7kXly5HTO9Tdhy7xi1I7hKEvL8qyBl5
TXT: 28.02.2018 00:28:02.00 | BMW Response | <US>?<BS><NUL><NUL><NUL><NUL><NUL><NUL><ETX>-▒1<LF>▒0<FF><ENQ>Ы|4?<RS> ▒▒<RS>?▒C?<DC3>▒▒▒<DC4>J▒ޡ]▒▒▒▒<BS><SI> ...
jetzt wird ein Token abgerufen
TXT: 28.02.2018 01:08:45.00 | BMW | token expired, get new token
TXT: 28.02.2018 01:08:45.00 | BMW Postfields | username=...
TXT: 28.02.2018 01:08:45.00 | BMW | access_token: wDvLMKjmuj5jAcA44K17GXAWc2FMSXIU
TXT: 28.02.2018 01:08:45.00 | BMW | Bearer expires in: 7199s
TXT: 28.02.2018 01:08:45.00 | BMW | set access_token: wDvLMKjmuj5jAcA44K17GXAWc2FMSXIU
TXT: 28.02.2018 01:08:45.00 | BMW | set token expiration: 1519783724
=> gueltig bis 03:08:44
ab hier wieder ok
TXT: 28.02.2018 01:18:46.00 | BMW | Send to url: https://www.bmw-connecteddrive.de/api/vehicle/dynamic/v1/WBAWY51xxxxxxxxxx?offset=-60
TXT: 28.02.2018 01:18:46.00 | BMW | 'Content-Type: application/json' , 'Authorization: Bearer ' wDvLMKjmuj5jAcA44K17GXAWc2FMSXIU
TXT: 28.02.2018 01:18:47.00 | BMW Response | {<LF> "attributesMap" : {<LF> "updateTime_converted" : "27.02.2018 17:35",<LF> "DCS_CCH_Ongoing " : null,<LF> ...
usw.
Abruf der Tokens, ca. alle 2:30h, Gültigkeit ist aber nur 2h (7199s):
TXT: 27.02.2018 22:25:00.00 | BMW | set token expiration: 1519773899
TXT: 28.02.2018 01:08:45.00 | BMW | set token expiration: 1519783724
TXT: 28.02.2018 03:38:46.00 | BMW | set token expiration: 1519792725
TXT: 28.02.2018 06:08:47.00 | BMW | set token expiration: 1519801726
TXT: 28.02.2018 08:20:08.00 | BMW | set token expiration: 1519809607
Ja, immer wenn der Token abgelaufen ist, kommt "Müll - erkennen kann man das daran, das der HTTP-Statuscocde 401 geliefert wird.
Also habe ich mir den Code angeschaut und folgende Änderungen angebracht:
$ git diff module.php
diff --git a/BMW/module.php b/BMW/module.php
index b3efc50..dc9f09e 100644
--- a/BMW/module.php
+++ b/BMW/module.php
@@ -533,7 +533,7 @@ class BMWConnectedDrive extends IPSModule
IPS_SetProperty($this->InstanceID, "token", $matches[1]);
$this->SendDebug("BMW","set access_token: ".$matches[1],0);
- $token_expiration = time() + $matches[3];
+ $token_expiration = time() + $matches[3] - 60;
IPS_SetProperty($this->InstanceID, "token_expiration", $token_expiration);
$this->SendDebug("BMW","set token expiration: ".$token_expiration,0);
IPS_ApplyChanges($this->InstanceID);
@@ -1277,6 +1277,8 @@ bmwSkAnswer=BMW_ACCOUNT_SECURITY_QUESTION_ANSWER
protected function SendBMWAPIV1($command, $action)
{
+ $this->CheckToken();
+
$area = $this->ReadPropertyInteger('bmw_server');
$api = $this->GetBMWServerURL($area);
$token = $this->ReadPropertyString("token");
@@ -1293,7 +1295,15 @@ bmwSkAnswer=BMW_ACCOUNT_SECURITY_QUESTION_ANSWER
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$response = curl_exec($ch);
- if($response === false)
+
+ $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if ($httpcode != 200)
+ $this->SendDebug("BMW Response", "got http-code $httpcode", 0);
+ if ($httpcode == 401)
+ {
+ $response = "";
+ }
+ else if ($response === false)
{
$curl_error = curl_error($ch);
$this->SendDebug("BMW","curl error: ".$curl_error,0);
@@ -1308,6 +1318,8 @@ bmwSkAnswer=BMW_ACCOUNT_SECURITY_QUESTION_ANSWER
protected function SendBMWAPI($command)
{
+ $this->CheckToken();
+
$area = $this->ReadPropertyInteger('bmw_server');
$api = $this->GetBMWServerURL($area);
$token = $this->ReadPropertyString("token");
@@ -1319,9 +1331,18 @@ bmwSkAnswer=BMW_ACCOUNT_SECURITY_QUESTION_ANSWER
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$response = curl_exec($ch);
- $this->SendDebug("BMW Response", $response,0);
$curl_error = curl_error($ch);
// $this->SendDebug("BMW","curl error: ".$curl_error,0);
+
+ $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if ($httpcode != 200)
+ $this->SendDebug("BMW Response", "got http-code $httpcode", 0);
+ if ($httpcode == 401 ) {
+ $response = "";
+ }
+ else {
+ $this->SendDebug("BMW Response", $response, 0);
+ }
curl_close( $ch );
return $response;
}
@@ -1623,4 +1644,4 @@ bmwSkAnswer=BMW_ACCOUNT_SECURITY_QUESTION_ANSWER
}
-?>
\ No newline at end of file
+?>
- fange ich den Fehler 401 ab, damit kein Müll in den Daten eingetragen wird.
- mache ich vor jeden Abruf ein CheckToken() und erneuere damit den Token, wenn abgelaufen
- verkürze ich die Gültigkeit des Token um 60 Sekunden, damit es keine raise-condition gibt (der Token ist noch 1s gültig, dann erfolgt der http-Request, der aber etwas braucht, bis er bei BMW eintrudelt …)
Damit ist doch auch dieser Timer überflüssig, oder?
$this->RegisterTimer('BMWTokenUpdate', 9000000, 'BMW_CheckToken('.$this->InstanceID.');');
Bisher hat die Änderung funktioniert. Was meinst Du?