orcasgit / python-fitbit

Fitbit API Python Client Implementation
Other
623 stars 330 forks source link

Error on Fitbit authentication when setting up permissions: 500 Internal Server Error #142

Closed aparente closed 5 years ago

aparente commented 5 years ago

When trying to set permissions for what data can be accessed by an application, I get a 500 Server Error

`500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last): File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 628, in respond self._do_respond(path_info) File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 687, in _do_respond response.body = self.handler() File "/anaconda3/lib/python3.6/site-packages/cherrypy/lib/encoding.py", line 219, in call self.body = self.oldhandler(*args, *kwargs) File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cpdispatch.py", line 54, in call return self.callable(self.args, self.kwargs) File "/Applications/Fitbit/python-fitbit/gather_keys_oauth2.py", line 50, in index self.fitbit.client.fetch_access_token(code) File "/Applications/Fitbit/python-fitbit/fitbit/api.py", line 146, in fetch_access_token code=code) File "/anaconda3/lib/python3.6/site-packages/requests_oauthlib/oauth2_session.py", line 307, in fetch_token self._client.parse_request_body_response(r.text, scope=self.scope) File "/anaconda3/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 415, in parse_request_body_response self.token = parse_token_response(body, scope=scope) File "/anaconda3/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response validate_token_parameters(params) File "/anaconda3/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters raise_from_error(params.get('error'), params) File "/anaconda3/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 405, in raise_from_error raise cls(kwargs) oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client) Powered by CherryPy 18.1.0`

Running this through a jupyter notebook using the following code:

server = Oauth2.OAuth2Server(CLIENT_ID, CLIENT_SECRET) server.browser_authorize() ACCESS_TOKEN = str(server.fitbit.client.session.token['access_token']) REFRESH_TOKEN = str(server.fitbit.client.session.token['refresh_token']) auth2_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET, oauth2=True, access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)

y26805 commented 5 years ago

hey I had a similar error. managed to solve it by setting the Callback URL (as well as what's in init in gather_keys_oauth2.py) to HTTPS. that way I could get the correct code to acquire an OAuth 2.0 token

skylergrammer commented 5 years ago

I was having a similar problem and was able to solve it by downgrading the oauth2-related libraries to the following versions: requests-oauthlib == 1.1.0 and oauthlib == 2.1.0. I haven't tried to diagnose why the 500 error is thrown yet.

dre2004 commented 5 years ago

I was having a similar problem and was able to solve it by downgrading the oauth2-related libraries to the following versions: requests-oauthlib == 1.1.0 and oauthlib == 2.1.0. I haven't tried to diagnose why the 500 error is thrown yet.

This also worked for me!

aparente commented 5 years ago

hey I had a similar error. managed to solve it by setting the Callback URL (as well as what's in init in gather_keys_oauth2.py) to HTTPS. that way I could get the correct code to acquire an OAuth 2.0 token

Tried this, now getting a 404 error at the same point (clicking OK after selecting all data I want to be accessed). FWIW, I also can't connect to https://127.0.0.1/ in either chrome or safari.

404 Not Found

Missing parameters: state

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cpdispatch.py", line 54, in __call__
    return self.callable(*self.args, **self.kwargs)
TypeError: index() missing 1 required positional argument: 'state'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 628, in respond
    self._do_respond(path_info)
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 687, in _do_respond
    response.body = self.handler()
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/lib/encoding.py", line 219, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cpdispatch.py", line 60, in __call__
    raise sys.exc_info()[1]
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cpdispatch.py", line 58, in __call__
    test_callable_spec(self.callable, self.args, self.kwargs)
  File "/anaconda3/lib/python3.6/site-packages/cherrypy/_cpdispatch.py", line 157, in test_callable_spec
    raise cherrypy.HTTPError(404, message=message)
cherrypy._cperror.HTTPError: (404, 'Missing parameters: state')
aparente commented 5 years ago

I was having a similar problem and was able to solve it by downgrading the oauth2-related libraries to the following versions: requests-oauthlib == 1.1.0 and oauthlib == 2.1.0. I haven't tried to diagnose why the 500 error is thrown yet.

This also worked for me!

Strange - I downgraded these two modules with pip and now I'm getting an error that it can' t find requests_oauthlib. I confirmed its installed in /anaconda3/lib/python3.6/site-packages.


ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-f3df68ffb3e9> in <module>()
----> 1 import fitbit
      2 import gather_keys_oauth2 as Oauth2
      3 import pandas as pd
      4 import datetime
      5 CLIENT_ID = '22D9V2'

/Applications/Fitbit/python-fitbit/fitbit/__init__.py in <module>()
      8 """
      9 
---> 10 from .api import Fitbit, FitbitOauth2Client
     11 
     12 # Meta.

/Applications/Fitbit/python-fitbit/fitbit/api.py in <module>()
     13 
     14 from requests.auth import HTTPBasicAuth
---> 15 from requests_oauthlib import OAuth2Session
     16 
     17 from . import exceptions

ModuleNotFoundError: No module named 'requests_oauthlib'````
andreimarinescu commented 5 years ago

I can also confirm that downgrading these libraries worked for me as well (I've used @dre2004's fork, thanks for that, by the way!).

andreimarinescu commented 5 years ago

Hey guys. Any updates on this? I can create a PR fixing the dependencies for requests-oauthlib and oauthlib if it helps, the library has been running without issues for me for about 2 weeks using these versions.

brad commented 5 years ago

@andreimarinescu That would be really helpful!

aparente commented 5 years ago

Ok, so this is now working!

  1. Set up conda environment running requests-oauthlib == 1.1.0 and oauthlib == 2.1.0. Installed the python-fitbit api through pip.
conda create --no-default-packages --name fitbit_api_test python=2.7
conda install -n fitbit_api_test python=2.7 oauthlib=2.1.0 sphinx coverage python-dateutil pip cherrypy pandas datetime ipython
conda activate fitbit_api_test

pip install fitbit
pip install requests-oauthlib==1.1.0
pip install oauthlib==2.1.0
  1. For whatever reason, it couldn't find the gather_keys_oauth2.py when I installed fitbit through pip. Had to create it manually as well as edit the redirect_uri to match my host set in my App.
paulhoff commented 5 years ago

Downgrading using: pip install requests-oauthlib==1.1.0 pip install oauthlib==2.1.0

worked for me too!

Thanks all.

brad commented 5 years ago

I think this is fixed with version 0.3.1 on PyPI. Please reopen if I'm wrong about this