Not renewing Token
Closed this issue · 11 comments
Hi, I've followed instructions. Everything goes well but after 20 min the cron job give me an error related to renewal of token I think.
When I run cron.php it gives me this error.
root@HAProxy-VM:/var/www/sqlbee# php -f cron.php
PHP Fatal error: Uncaught Exception: Could not grant token in /var/www/sqlbee/sqlbee.php:219
Stack trace:
#0 /var/www/sqlbee/sqlbee.php(123): sqlbee\sqlbee->refresh_token('F8tSzCQ37N8slBV...')
#1 /var/www/sqlbee/sqlbee.php(246): sqlbee\sqlbee->ecobee('GET', '/1/thermostatSu...', Array)
#2 /var/www/sqlbee/cron.php(14): sqlbee\sqlbee->get_thermostat_summary()
#3 {main}
thrown in /var/www/sqlbee/sqlbee.php on line 219
Regards
What seems to be happening is that your access token is expiring (which is normal; they expire every hour ಠ_ಠ) but the API call that refreshes it is failing for some reason. The exception you're getting is saying that the results sqlbee needs from that API call are missing.
Inside of your configuration.php
, could you set public static $debug = true;
? This will log the API calls to the api_log
table in your database.
Then, if you can reproduce that error with logging enabled please post the row in the api_log
table for that. You're looking for a post
to endpoint token
. The arguments
column should contain values like grant_type
and refresh_token
.
Thanks!
Also having the same issue, new install everything else works great for the first hour. Will do the above and post the results when it fails again (just re-authed manually)
It's not adding anything to the api_log table when cron.php fails but manually running it gives this error:
PHP Fatal error: Uncaught Exception: Could not grant token in /sqlbee/sqlbee.php:226
Stack trace:
#0 /sqlbee/sqlbee.php(123): sqlbee\sqlbee->refresh_token('Rkk3...')
#1 /sqlbee/sqlbee.php(261): sqlbee\sqlbee->ecobee('GET', '/1/thermostatSu...', Array)
#2 /sqlbee/cron.php(14): sqlbee\sqlbee->get_thermostat_summary()
#3 {main}
thrown in /sqlbee/sqlbee.php on line 226
Running cron.php manually doesn't throw this error until the grant_token starts to fail.
I am able to duplicate this issue. It seems like the ecobee API is failing to refresh your credentials even when I am sending the request correctly and at the right time. I'm still investigating but I'm inclined to think this is some bug with their API as this has never before been a problem and nothing about the authorization responses seems to have changed to indicate I need to do anything differently.
I've temporally worked around it by refreshing the token at the start of cron.php file just before
$response = $sqlbee->get_thermostat_summary();
Not really sure why it worked but it's been going fine for a couple hours now using this method.
You're "officially" supposed to wait until you get a "token expired" response from the API before you renew it, but that's what seems broken at the moment. Doing it your way shouldn't hurt anything - it just gets a new token every time the cron job runs so it never has a chance to expire.
Once I can reliably duplicate the renew after expiration issue I'll post a report to their developer forums to try and figure it out. Unfortunately I have to wait an hour every time. :) Thanks for the extra info @mahodder!
Thanks. Could you detail the part of the code you need to move, please. Thanks.
I have identified the issue and will be pushing a fix sometime in the next few days. This will address the failure to properly renew a token and also the fact that exceptions are preventing rows from getting inserted into the api_log table.
This is now fixed. If you pull the latest code everything should be all set. Please note that you will also need to create the api_log
[correction: error_log
] table manually unless you completely recreate your database using the sqlbee.sql
script.
`create` table `error_log` (
`error_log_id` int(10) unsigned not null auto_increment,
`timestamp` timestamp not null default current_timestamp,
`json_error` text not null,
`deleted` tinyint(1) unsigned not null default '0',
primary key (`error_log_id`)
) engine=innodb default charset=utf8 collate=utf8_unicode_ci;`
To detail the issue: The renew token API call was working fine. However, there was a bug in the code that re-calls the original failed API call. That bug threw an exception which rolled back the transaction and thus, the new token was never properly created in the database. Subsequent attempts at renewing the token would then fail because the refresh token was already used. I fixed this bug and also made the error/exception logging much more robust to prevent this type of problem in the future. As a nice side-effect, you can now reference the error_log table to watch for problems without needing to manually execute the cron script.
It's working great, I believe you meant "create the error_log table manually" instead of "api_log" just in case that throws someone off :)
I'm using this to pull in the temperature from Ecobee and it's various sensors around my house to automatically decide when to turn some heaters on / off using iHome plugs (https://raszewski.info/2017/05/04/hacking-ihome-isp5-smart-plug/)
Cool! I'm glad this is serving someone. :)
Let me know if you have any particular feature requests. I intend on eventually adding sensor history (#7). I have also built API calls in the past to control the ecobee instead of just reading from it, but I'm not sure if/how that fits into this particular project.
Edit: Also fixed my post above. Thanks!