jrupac / tasky

A command-line desktop client for Google Tasks.
GNU General Public License v3.0
114 stars 33 forks source link

script cannot recover from oauth token refresh fail, requires manual fix #30

Closed phpmoli closed 6 years ago

phpmoli commented 6 years ago

Hello @jrupac ,

I have a script which backs up my gtasks in the text form outputed by tasky (by the way: json or native unformatted output possible?) which runs every hour, and i have received these 2 emails from that script:

Traceback (most recent call last): File "/home/user/bin/tasky.py", line 547, in main(sys.argv) File "/home/user/bin/tasky.py", line 519, in main tasky.GetData() File "/home/user/bin/tasky.py", line 279, in GetData tasks = self.service.tasks().list(tasklist=tasklist['id']).execute() File "/usr/local/lib/python2.7/dist-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, *kwargs) File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 835, in execute method=str(self.method), body=self.body, headers=self.headers) File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 162, in _retry_request resp, content = http.request(uri, method, args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/oauth2client/transport.py", line 186, in new_request credentials._refresh(orig_request_method) File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 761, in _refresh self._do_refresh_request(http) File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 819, in _do_refresh_request raise HttpAccessTokenRefreshError(error_msg, status=resp.status) oauth2client.client.HttpAccessTokenRefreshError: internal_failure: Backend Error ERROR

and then

usage: tasky.py [--auth_host_name AUTH_HOST_NAME] [--noauth_local_webserver] [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]] [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] tasky.py: error: unrecognized arguments: --nocolor --list ERROR

every hour.

I think what happened is that the script could not get a new token from Google and after this it got into a state from which it can not recover from: it now needs manual re-authorization from the admin via logging in to the server and fixing this problem.

Could you please help me fixing this? Thank you!

jrupac commented 6 years ago

Hm, so there a few things here.

  1. As you diagnosed, there was a failure in credentials._refresh() which calls _do_refresh_request if there is no Storage object (the tasks.dat file) is found. In this case, the access token is expired. For whatever reason the refresh request failed and perhaps the on-disk Storage file had unexpected contents written to it (or just marked as invalid) causing problems in future invocations. Unfortunately I think here you may need to do a reauth flow: delete the keys.txt and tasks.dat file wherever you put them (default $HOME/.tasky/) and re-run tasky to generate a new access token. There's not much Tasky can do here I think because token management is handled entirely by the API client library. Just out of curiosity, what does your tasks.dat file say? (Don't include the full contents here, I'm just wondering if there's any indication that the credentials are invalid or an error message).

  2. I'm not sure where that "usage:" line is coming from, since of course --nocolor and --list are flags for Tasky. The auth code (the failure code path) tries to read a flag parsed by argparse and since the Tasky-specific flags haven't been parsed yet, it fails. I should move flag parsing earlier on which should address that. I wonder if you would've gotten a more reasonable error message if flag parsing hadn't failed. I will do that.

  3. I'll open a separate request for a more machine-parseable output (pull requests encouraged!).

jrupac commented 6 years ago

Pushed d03f9f67a7ab649c554b2d45d602512b6ab2cbe2

phpmoli commented 6 years ago

Thanks for the reply!

and perhaps the on-disk Storage file had unexpected contents written to it (or just marked as invalid) causing problems in future invocations [...] Just out of curiosity, what does your tasks.dat file say?

Yeah, i guessed that happened. I tried to reproduce the bug by editing /usr/local/lib/python2.7/dist-packages/oauth2client/client.py line 782, but did not succeed. I do not have the .dat anymore, i will save, anonimize, reopen and attach next time this happens.