mk-fg / python-onedrive

Obsolete python/cli module for MS SkyDrive/OneDrive's old API, do not use for new projects
Do What The F*ck You Want To Public License
199 stars 32 forks source link

AttributeError: 'Response' object has no attribute 'json' #33

Closed horsey2014 closed 9 years ago

horsey2014 commented 9 years ago

=== I just installed onedrive-cli but every command I make comes up with the following error: AttributeError: 'Response' object has no attribute 'json'

=== For example: $ onedrive-cli auth Visit the following URL in any web browser (firefox, chrome, safari, etc), authorize there, confirm access permissions, and paste URL of an empty page (starting with "https://login.live.com/oauth20_desktop.srf") you will get redirected to in the end. Alternatively, use the returned (after redirects) URL with "/usr/local/bin/onedrive-cli auth " command.

URL to visit: https://login.live.com/oauth20_authorize.srf?scope=wl.skydrive+wl.skydrive_update+wl.offline_access&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf&response_type=code&client_id=0000000048xxxxxx

URL after last redirect: https://login.live.com/oauth20_desktop.srf?code=xxxxxxxx-aed1-11e0-b4be-xxxxxxxxxxxx&lc=xxxx Traceback (most recent call last): File "/usr/local/bin/onedrive-cli", line 9, in load_entry_point('python-onedrive==14.11.0', 'console_scripts', 'onedrive-cli')() File "/usr/local/lib/python2.7/dist-packages/onedrive/cli_tool.py", line 263, in main api.auth_get_token() File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 539, in auth_get_token ret = super(PersistentOneDriveAPI, self).auth_get_token(_argz, *_kwz) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 238, in auth_get_token res = self.auth_access_data_raw = self._auth_token_request() File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 271, in _auth_token_request return self.request(self.auth_url_token, method='post', data=post_data) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 173, in request res = res.json() AttributeError: 'Response' object has no attribute 'json'

=== Here is my .lcrc, it seemed to have put the auth code into the .lcrc correctly. I don't know if I should put the "-MG1SRN" after the secret but it did not seem to matter either way. auth: code: xxxxxxxx-aed1-11e0-b4be-xxxxxxxxxxxx client: id: 0000000048xxxxxx secret: xxxxgGtrgkbbx5n3xbxxxxxxxxxxxxx-MG1SRN

=== Since the .lcrc looked correct I tried the quota command and also get the same error: $ onedrive-cli --debug quota DEBUG:onedrive.api_v5:Requesting new access_token through authorization_code grant INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): login.live.com DEBUG:requests.packages.urllib3.connectionpool:"POST /oauth20_token.srf HTTP/1.1" 400 127 Traceback (most recent call last): File "/usr/local/bin/onedrive-cli", line 9, in load_entry_point('python-onedrive==14.11.0', 'console_scripts', 'onedrive-cli')() File "/usr/local/lib/python2.7/dist-packages/onedrive/cli_tool.py", line 267, in main df, ds = map(size_units, api.get_quota()) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 502, in get_quota super(OneDriveAPI, self).get_quota())) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 357, in get_quota return self('me/skydrive/quota') File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 349, in call self.auth_get_token() File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 539, in auth_get_token ret = super(PersistentOneDriveAPI, self).auth_get_token(_argz, *_kwz) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 238, in auth_get_token res = self.auth_access_data_raw = self._auth_token_request() File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 271, in _auth_token_request return self.request(self.auth_url_token, method='post', data=post_data) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 173, in request res = res.json() AttributeError: 'Response' object has no attribute 'json'

mk-fg commented 9 years ago

Thanks for reporting it.

That traceback indeed looks like a bug in processing non-json error responses from OneDrive API (not sure if they're supposed to be there tbh, might be an issue with API as well), which should be fixed in the latest git/pypi version.

Though regardless of how those error responses get processed, there's still an error ("400 Bad Request") returned from OneDrive API, as indicated by this line:

 DEBUG:requests.packages.urllib3.connectionpool:"POST /oauth20_token.srf HTTP/1.1" 400 127

Your client-id and other auth data looks roughly like the one that got returned to me as well, so not sure where that error might come from, but if you'll run the latest version (can get the file from github or just do pip install --upgrade ... from pypi), hopefully it might give you a more useful error message (please paste it here btw, even if you'll figure out how to resolve it - might help someone else).

There was actually a kinda-similar recent issue report in #32, and apparently just cleaning up browser cookies and re-doing the whole auth dance helped the person there, though given just http-400 code, it can be a totally different thing for you.

horsey2014 commented 9 years ago

I did the pip upgrade and make a bit more progress. The http request is successful with a 200 code. but the following is the error message to the auth request.

DEBUG:onedrive.api_v5:Requesting new access_token through authorization_code grant INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): login.live.com DEBUG:requests.packages.urllib3.connectionpool:"POST /oauth20_token.srf HTTP/1.1" 200 1424 Traceback (most recent call last): File "/usr/local/bin/onedrive-cli", line 9, in load_entry_point('python-onedrive==14.11.2', 'console_scripts', 'onedrive-cli')() File "/usr/local/lib/python2.7/dist-packages/onedrive/cli_tool.py", line 263, in main api.auth_get_token() File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 540, in auth_get_token ret = super(PersistentOneDriveAPI, self).auth_get_token(_argz, *_kwz) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 239, in auth_get_token res = self.auth_access_data_raw = self._auth_token_request() File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 272, in _auth_token_request return self.request(self.auth_url_token, method='post', data=post_data) File "/usr/local/lib/python2.7/dist-packages/onedrive/api_v5.py", line 168, in request return json.loads(res.text) if not raw else res.content AttributeError: 'Response' object has no attribute 'text' DEBUG:onedrive.conf:Updating configuration file ('/home/les/.lcrc')

mk-fg commented 9 years ago

Can you post the output of a pip freeze | grep requests command?

Both errors above seem to indicate different API in "requests" python module on your system, and I'm not actually sure how old it should be to be missing a "text" attribute (as last traceback indicates). Also, I'd think pip might update the thing anyway with --upgrade, but not sure.

Anyhow, if the version of that module that you have is indeed super-old to the point of not working, guess I can add a more descriptive error message when it gets detected.

I think the earliest versions of "requests" I've used with the module were 0.13.x , which should be 2+ years old by now, and they had both "text" and "json" attrs/methods. If above command will indeed show an older version, doing pip install --upgrade requests (adding --user flag or in a venv, as necessary) might resolve the issue.

mk-fg commented 9 years ago

Alternatively, as of dc1590f (and 14.11.3 version of python-onedrive), that version should be printed with --debug option, i.e.:

% ./onedrive-cli --debug ls
DEBUG:onedrive.api_v5:Using "requests" module version: '2.3.0'
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): apis.live.net
DEBUG:requests.packages.urllib3.connectionpool:"GET /v5.0/me/skydrive/files?access_token=EwB4... HTTP/1.1" 200 979
...
horsey2014 commented 9 years ago

Your guess was right using the latest onedrive I could see I had an extremely old requests package: DEBUG:onedrive.api_v5:Using "requests" module version: '0.8.2'

I updated, as instructed, requests to the latest 2..4.3 and it works now.

Thanks for all your help in resolving this issue.

mk-fg commented 9 years ago

No problem.

I think it's a legitimate issue that there's no check for that low version number (even though there are some version-specific code) when module is not pulled-in as a dep, plus when it is, there should probably version conditional as well. Guess I'll set that minimum to 0.14.0, as even though older ones might work, it seem unlikely that many people would use them (and actually need to use them).