Exceptions - 401 in particular
lipkau opened this issue · 12 comments
Hello.
first of all, thank you for this package! cool stuff :-)
I got to the point where I am implementing the exceptions of the APIs I am using.
But I am now stuck with the problem:
How can I identify if the case where the access_token was invalidated?
For the other APIs I am consuming, I am checking for 401 responses.
What is the recommended way here?
if (!$client->getAllProjects()) {
removePairing();
}
I was expecting something more in the likes of
try {
$client->getAllProjects();
} catch (\FabianBeiner\Todoist\ResponseForbidden $e) {
removePairing();
}
Hey @lipkau,
Thanks for the kind words!
At the moment, I did not consider the scenario that the Token gets changed while the script is running. This is because it's not an OAuth token, but a manually and one time generated one. While people might regenerate one, it was not likely, in my opinion, that this happens while the script does anything.
So for now, I'd recommend the following:
try {
$Todoist = new FabianBeiner\Todoist\TodoistClient('API_TOKEN');
if ( ! $Todoist->getAllProjects()) {
throw new Exception('The API token seems invalid.');
}
} catch (Exception $e) {
echo 'An error occurred: ' . $e->getMessage();
return false;
}
This is not elegant, but it might be a solution for now. I'll have to add checks anytime soon.
Thank you.
To clarify, I am not counting on the token being invalidated during runtime.
But rather having the app use the OAuth Authorization Code Flow for pairing with todoist, storing the access_token in a DB and using it later.
the token can get invalidated by
- the App registered at todosit being deleted
- the app owner invalidating all tokens
- the user removing the integration with the app https://todoist.com/prefs/integrations
I am using the REST API, not the Sync API, so there is no OAuth, just a personal API token. ;)
I know. But the REST API can use the same OAuth as the sync API:
https://developer.todoist.com/rest/v1/#authorization links to https://developer.todoist.com/sync/v8/#oauth
Yes, but as said, this class does not.
It does!
I can show you, if you are interested
Of course, go ahead!
do you want a sample repo which you have to clone, setup and create a todoist app to test?
I can also show you via discord or zoom
Wait, just to be sure:
You implemented that OAuth thingy yourself, right?
correct
Ahhh! Well, you have to implement this for yourself at the moment, as my official library does not support this. But I'm looking forward to the updated library! :)
For anyone interested in this: @FabianBeiner 's suggestion on how to solve this did not take into account that an empty array is also considered false. Therefore, I recommend using this instead:
try {
$Todoist = new FabianBeiner\Todoist\TodoistClient('API_TOKEN');
if ( ! is_array($Todoist->getAllProjects())) {
throw new Exception('The API token seems invalid.');
}
} catch (Exception $e) {
echo 'An error occurred: ' . $e->getMessage();
return false;
}