Python fitbit api

Just spent a couple of hours trying to use the fitbit python API following a towardsdatascience (https://towardsdatascience.com/collect-your-own-fitbit-data-with-python-ff145fa10873) article but kept getting the following error:

Traceback (most recent call last):
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 630, in respond
    self._do_respond(path_info)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 689, in _do_respond
    response.body = self.handler()
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 221, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/cherrypy/_cpdispatch.py", line 54, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "/home/mick/git/DSI_QS/Quantified Self/gather_keys_oauth2.py", line 50, in index
    self.fitbit.client.fetch_access_token(code)
  File "/home/mick/git/DSI_QS/Quantified Self/fitbit/api.py", line 146, in fetch_access_token
    code=code)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/requests_oauthlib/oauth2_session.py", line 307, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 415, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response
    validate_token_parameters(params)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/home/mick/anaconda3/envs/opendata-grabber/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 405, in raise_from_error
    raise cls(**kwargs)
InvalidClientError: (invalid_client)

Eventually, I came across the following comment in the issues on github: https://github.com/orcasgit/python-fitbit/issues/142#issuecomment-476053642

So you need to make sure you set up your conda environment properly and install the correct version of requests-oauthlib & oauthlib, and install the python-fitbit API through pip.

Like this:

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

Copy the “gather_keys_oauth2.py” file from the fitbit repository  (here: https://github.com/orcasgit/python-fitbit) to you PyCharm solution and you should be good to go.

Then create a new PyCharm project pointing at the newly created conda environment.

Leave a comment