Core: Fitbit Callback-URL during OAuth-flow does not use https

Created on 11 Dec 2018  路  42Comments  路  Source: home-assistant/core

Home Assistant release with the issue: 0.83.1

Last working Home Assistant release (if known): unknown

Operating environment (Hass.io/Docker/Windows/etc.): Hassio

Component/platform: sensor.fitbit

Description of problem: Even if the base_URL is set to include an explicit protocol (https in this case), the authorization-call to the Fitbit-API does not include said protocol. New fitbit apps are required to use https in the callback

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

http:
  # Secrets are defined in the file secrets.yaml
  api_password: !secret http_password
  # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
  base_url: https://ha.pc-coholic.de
  ip_ban_enabled: true
  login_attempts_threshold: 1

sensor:
  - platform: fitbit
    clock_format: 24H
    unit_system: metric
    monitored_resources:
      - "body/weight"
      - "body/bmi"

Additional information:
In the second step of the Fitbit Authorization, the user is asked to click a link like IP/api/fitbit.
image

This link redirects to the Fitbit OAuth Authorization-screen.

However, if a https-URL is set as HomeAssistant's base_url, the call fails, as the redirect_uri-parameter is not populated with the https from base_url. As a consequence, the call fails and HA cannot be authorized, as the non-HTTPS-URI is not in Fitbit's list of acceptable callback-URI.

Quick Fix/Workaround: Manually append the missing s to redirect_uri.

Couldn't necro https://github.com/home-assistant/home-assistant/issues/15236 so I copied his issue as mine is the same.

Changing the redirect url to include https in the fibit auth page does not work since HA stores the url locally as http. After changing the url https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=XYZ&redirect_uri=https%3A%2F%2Fme.duckdns.org%2Fapi%2Ffitbit%2Fcallback&scope=activity+heartrate+weight&state=ABC to include https, I get a fitbit permission screen, but after saving I get a 500 from HASS

fitbit

Most helpful comment

I seem to be having this issue, or a similar one as well on 0.85.1.
I'm running on Hassio and I have the Let's Encrypt addon. My router is port-forwarding 443 to my Hassio RPi.

I've got my app set up with my domain name and https endpoint. The permissions screen on fitbit.com shows up correctly. When redirecting to hass, I get a 500 Error. The log mentions the stack trace below. Should I open a separate issue?

2019-01-20 18:35:30 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 406, in start
    resp = await task
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_app.py", line 435, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 120, in impl
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/static.py", line 66, in staticresource_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/real_ip.py", line 34, in real_ip_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/ban.py", line 67, in ban_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/auth.py", line 99, in auth_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/view.py", line 115, in handle
    result = handler(request, **request.match_info)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/fitbit.py", line 333, in get
    redirect_uri)
  File "/usr/local/lib/python3.6/site-packages/fitbit/api.py", line 146, in fetch_access_token
    code=code)
  File "/usr/local/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 "/usr/local/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 "/usr/local/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response
    validate_token_parameters(params)
  File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/usr/local/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) 

All 42 comments

Confirmed this fix works:
https://github.com/home-assistant/home-assistant/blob/994b829cb4b706d0103598b194da9a36910cd2f5/homeassistant/components/sensor/fitbit.py#L174

hard coded my base url with https and we are good. Now I have an issue where I imported Fitbit sensors twice by accident - the first time I successfully authed it did not display a page in browser, but downloaded callback - so I missed the fact that it finished ok.

How can I remove the duped sensors? EDIT - see that they are in sqlite db, copying to my pc to edit now since hassos has no sqlite tools

I seem to be having this issue, or a similar one as well on 0.85.1.
I'm running on Hassio and I have the Let's Encrypt addon. My router is port-forwarding 443 to my Hassio RPi.

I've got my app set up with my domain name and https endpoint. The permissions screen on fitbit.com shows up correctly. When redirecting to hass, I get a 500 Error. The log mentions the stack trace below. Should I open a separate issue?

2019-01-20 18:35:30 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 406, in start
    resp = await task
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_app.py", line 435, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 120, in impl
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/static.py", line 66, in staticresource_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/real_ip.py", line 34, in real_ip_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/ban.py", line 67, in ban_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/auth.py", line 99, in auth_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/view.py", line 115, in handle
    result = handler(request, **request.match_info)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/fitbit.py", line 333, in get
    redirect_uri)
  File "/usr/local/lib/python3.6/site-packages/fitbit/api.py", line 146, in fetch_access_token
    code=code)
  File "/usr/local/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 "/usr/local/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 "/usr/local/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response
    validate_token_parameters(params)
  File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/usr/local/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) 

I have the same exact problem. Is there a solution to it?

Hey guys. I can confirm this is still an issue on 0.86.3. Is there anything I can do to offer more information regarding what's going on? Is it worth testing this issue on a Hassbian installation instead of a Hassio one?

So I've been able to create a custom component from the fitbit.py component in the mainline code by dropping it in /config/custom_components/sensor/fitbit.py. As per the stack trace above, the issue appears on line 333, where the python-fitbit dependency is getting called for obtaining a long-term access token.

As it happens, this exact issue is the top issue reported on the Github repo of python-fitbit (https://github.com/orcasgit/python-fitbit/issues/142).

Would I be able to test the work-around mentioned there on my current setup? I've noticed that there is a /config/deps but I'm not sure how to set custom module dependencies in order to override the default ones on Hassio. I've done this before on Hassbian by activating the virtual env.

Okay, so I was able to finally work around this issue. This isn't a fix (yet), however when a new version of python-fitbit is published I'll submit a PR fixing this in mainline.

The bug is indeed the one I've referenced above, and fixing the requirements to those versions actually allows for the authorization to work correctly from end to end.

For now, I'm running off a custom component I've installed in /config/custom_components/sensor/fitbit.py. The only change is that the REQUIREMENTS array no longer references fitbit==0.3.0, but a fork with updated dependencies (not mine, I've just stumbled upon it via the Github Network tab).

In case anyone once to get the platform running in the meanwhile, here's a gist of my custom component: https://gist.github.com/andreimarinescu/8c0d556b5e4c54adfec99fcd9e3cb8a2

Hi @andreimarinescu , I am using your gist as custom component. But still getting the same stack trace.
I noticed you log data.get('code'), mine is changing on every new authorization try.
Any idea?

2019-02-03 18:37:56 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/static.py", line 66, in staticresource_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/real_ip.py", line 34, in real_ip_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/ban.py", line 67, in ban_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/auth.py", line 99, in auth_middleware
    return await handler(request)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/http/view.py", line 115, in handle
    result = handler(request, **request.match_info)
  File "/config/custom_components/sensor/fitbit.py", line 334, in get
    redirect_uri)
  File "/usr/local/lib/python3.6/site-packages/fitbit/api.py", line 146, in fetch_access_token
    code=code)
  File "/usr/local/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 "/usr/local/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 "/usr/local/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response
    validate_token_parameters(params)
  File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/usr/local/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) 

Hi, @yahel2410,

The code should be changing on each auth try. The log is not really needed, I just used to determine if Home Assistant was using my own version of the component. Are you seeing that log in your Home Assistant logs? If you're seeing the log but things are still not working, I'm afraid I'm not sure what's going on.

Some things to check:

  • are you running Hassio? The instructions would be different for Hassbian or other envs
  • did you restart HA after creating the component?
  • do you see the fitbit auth code logged in your home assistant log before the exception happens? (you can access the logs from the UI by tapping on the information pictogram at the bottom of the menu)
  • in case your custom component isn't getting called, and you're running Hassio, where did you install the file?

Hi @andreimarinescu, I had to recreate the app on Fitbit site and replace fitbit.conf to make it work (with your gist)

Thanks, that was very helpful!

@andreimarinescu Thank you for the workaround. I can also confirmed it worked for me too

I can confirm the workaround works on 0.86.3 on hassbian too. Thanks!

Hi @andreimarinescu . I'm running HASSIO 0.90.1 and I've uploaded your python script /config/custom_components/sensor/fitbit.py as directed and restarted HA. I've tried to re-authenticate with the Fitbit site however when it redirects I am still getting a 500 Internal Server Error. Are there other actions that I am missing?

Hey, @Sooty70. The home assistant custom components structure has shifted since I created those instructions. The correct path should now be fitbit/sensor.py instead of sensor/fitbit.py

Thanks @andreimarinescu - worked a treat!

hmm in my case there is no possibility.

  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/sensor/fitbit.py", line 498, in <module>
    save_json(self.config_path, config_contents) NameError: name 'self' is not defined

how did you upload it into hassio in a docker container?

@martykiico I'm on the latest version of HASSIO and In my config folder I created the folder structure "custom_components/fitbit" and then copied @andreimarinescu code here renaming the file to sensor.py and then restarted HA.

@Sooty70 same result:

do you have customized some lines?

Error executing service <ServiceCall homeassistant.restart 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1147, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1160, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/hassio/__init__.py", line 247, in async_handle_core_service
    errors = await conf_util.async_check_ha_config_file(hass)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/config.py", line 785, in async_check_ha_config_file
    check_ha_config_file, hass)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/scripts/check_config.py", line 371, in check_ha_config_file
    platform = loader.get_platform(hass, domain, p_name)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/loader.py", line 97, in get_platform
    base_paths)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/loader.py", line 166, in _load_file
    module = importlib.import_module(path)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/fitbit/sensor.py", line 498, in <module>
    save_json(self.config_path, config_contents)
NameError: name 'self' is not defined

Edited. Solved my issue below: if I select to authorize access to all fitbit parameters (which is what I tried the first try it did sometime ago). Using same settings, the workaround did it ok. Thanks!


I'm running on Hassio. I created sesor.py at config\custom_components\fitbit following the avobe indications. When autorization, I still get the 500 error and two logs.
Not sure if I'm missing anything there. Any assistance would be much appreciated.

1- Test d8ace273027a05778c6dd54b299c412a99d48675 https://myuser.duckdns.org:8123/api/fitbit/callback

2-

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/real_ip.py", line 33, in real_ip_middleware
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/ban.py", line 68, in ban_middleware
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/auth.py", line 206, in auth_middleware
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/view.py", line 109, in handle
    result = handler(request, **request.match_info)
  File "/config/custom_components/fitbit/sensor.py", line 334, in get
    redirect_uri)
  File "/usr/local/lib/python3.7/site-packages/fitbit/api.py", line 146, in fetch_access_token
    code=code)
  File "/config/deps/lib/python3.7/site-packages/requests_oauthlib/oauth2_session.py", line 244, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 411, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 379, in parse_token_response
    validate_token_parameters(params)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 409, in validate_token_parameters
    raise w
Warning: Scope has changed from "settings weight profile activity nutrition heartrate sleep" to "settings heartrate activity sleep".

That's an important note for anyone having issues, I've seen this reported on the python-fitbit repository as well. Welcome aboard to the working fitbit integrations @fgsalvador :)

@Sooty70 same result:

do you have customized some lines?

Error executing service <ServiceCall homeassistant.restart 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1147, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1160, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/hassio/__init__.py", line 247, in async_handle_core_service
    errors = await conf_util.async_check_ha_config_file(hass)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/config.py", line 785, in async_check_ha_config_file
    check_ha_config_file, hass)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/scripts/check_config.py", line 371, in check_ha_config_file
    platform = loader.get_platform(hass, domain, p_name)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/loader.py", line 97, in get_platform
    base_paths)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/loader.py", line 166, in _load_file
    module = importlib.import_module(path)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/fitbit/sensor.py", line 498, in <module>
    save_json(self.config_path, config_contents)
NameError: name 'self' is not defined

@martykiico - No customisation beyond what was in @andreimarinescu file.

Hi @andreimarinescu - I've been struggling with this for weeks now, so thought I might be able to get some help from you. I've tried copying your file into custom_components/fitbit/sensor.py but I don't think HA is recognizing this custom component as I don't see the warning when HA starts. Is there any way to check if this is the case? My config.yaml has the following entry:

sensor:
  - platform: fitbit
    monitored_resources:
      - "activities/tracker/steps"
      - "devices/battery"

Once I see the notification to configure Fitbit, I go through the same issue as everyone else where I get the 500 server error at the final step. Really going nowhere with this, so would appreciate any help. Thanks!

Hi, @aazimh. Not sure what's going on. When authorizing Fitbit did you check all permission boxes? It seems to be needed. What version of Home Assistant and Hass.io are you running?

Yes, checked all boxes. I'm on the latest Home Assistant, but not using Hass.io. Could that be a factor? I just have it set up in a virtual env.

Hi,

I have the same issue with the redirect uri. I recently added home assistant cloud and used the https url in fitbit redirect uri but i get the same issue. One another thing i noticed was that in the authentication url where the error is thrown, i see redirect uri as http://localhost:8123 instead of my https url. I dont know where it comes from.
So, i tried the way suggested by @andreimarinescu . I created the folder structure custom_components/fitbit/sensor.py and added
fitbit:
as new component but i getting error "invalid config". I am using ha version 0.90.2. Could you please let me know if i recreated the right steps ? thanks

@aazimh - these instructions are for Hass.io unfortunately. You'd have to look into how to set up custom components for your own installation method. The code should still work, but you might have to install the dependencies manually.

@koushikv04 not sure what's going on. I'd recommend checking out the configuration of the fitbit app and the redirect URI set in there. Also the base URLe settings in your configuration.yaml.

Hi @andreimarinescu mine too is hassbian and the basic url is not changed because i have activated the home assistant cloud hence, it didn't modify anything in my config file but gave me a public url. I gave the public url in the redirect uri in Fitbit but the home assistant is not displaying the public url, it only displays the local host. What do you think i should change?

So first of all, you need to check how you can override components in hassbian. I haven't had a chance to do this thus far, all my instances are Hass.io based. AFAIK you should be able to overwrite the actual component as you should have direct access to it, but you'll need to research the proper paths.

Also, the only change to the component are the versions of the library dependencies, you might be able to get away with just pip installing the correct versions in the home assistant virtualenv.

Regarding the base URL, please note that I'm using letsencrypt for getting a SSL certificate and I'm using my own domain, but here's how my configuration looks:

http:
  base_url: https://<your_domain>
  cors_allowed_origins:
    - https://google.com
    - https://www.home-assistant.io
    - https://<your_domain>
    - https://<your_internal_ip>:8123

@andreimarinescu Is the workaround still working for you on the latest release of HA (0.92.2)? It has stopped working on my setup.

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 126, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/fitbit/sensor.py", line 241, in setup_platform
    authd_client.client.refresh_token()
  File "/usr/local/lib/python3.7/site-packages/fitbit/api.py", line 157, in refresh_token
    auth=HTTPBasicAuth(self.client_id, self.client_secret)
  File "/usr/local/lib/python3.7/site-packages/requests_oauthlib/oauth2_session.py", line 374, in refresh_token
    self.token = self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/usr/local/lib/python3.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 "/usr/local/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response
    validate_token_parameters(params)
  File "/usr/local/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/usr/local/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 405, in raise_from_error
    raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant) 

I've tried to setting it up again using the workaround and without but receive the 500 error when trying to authorize the callback.

Also having this issue with latest release, 500 error and oauth failing.

Run into the same issue, trying the custom component workaround too, and finally figured it out. @andreimarinescu has pointed out a good point. Just gotta update required packages, which are: oauthlib and requests-oauthlib to the newest release.

This is what I added to the custom component

REQUIREMENTS = ['requests-oauthlib==1.2.0', 'oauthlib==3.0.1']

Updated directory for adding fitbit custom component

/config/custom_components/fitbit/sensor.py

In case you need the file, I added a copy here:
https://gist.github.com/an-pham/f21b6ad8c571d5cc3ff11461a724d4dc

Am I missing something? I copied the file provided in the previous comment, checked for the updated requirements code snippet, added my text to the config.yaml, and I still get the 500 error.

Am I missing something? I copied the file provided in the previous comment, checked for the updated requirements code snippet, added my text to the config.yaml, and I still get the 500 error.

@dibertmatthew, also running into the 500 error and tried the suggestion from @an-pham (thank you for suggesting this). Also tried recreating the app on the fitbit side (an earlier comment recommended that).

I run this in docker and use haproxy (so, a lot of variables involved in this). A bit of a time crunch now, but I'll look into this more deeply when I have time.

If you have any logs related, please post @dibertmatthew.

I couldn't get it to work using the file @an-pham provided either. But it got me thinking with the requirements and the change to using manifest.json for components.

I'm running 0.93.1. Here is what I did:

I copy the files from https://github.com/home-assistant/home-assistant/tree/master/homeassistant/components/fitbit to /config/custom_components/fitbit

I modified manifest.json to include "requests-oauthlib==1.1.0" and "oauthlib==2.1.0"
"requirements": [ "fitbit==0.3.0", "requests-oauthlib==1.1.0", "oauthlib==2.1.0" ],

I restarted Hass.io twice and fitbit callback worked, no 500 error.

Possible points you can debug, hope it helps:

  • I removed "fitbit==0.3.0" from reference
  • Take a look into: /config/home-assistant.log to make sure your custom_component is loaded (where the exception has thrown should be: /config/custom_components/fitbit/sensor.py)
  • Since now you have your own component, maybe you can debug to see exactly what happened

Here's how I set up to debug:
Added to #configuration.yaml

logger:
  default: info
  logs:
      custom_components.fitbit: debug

And in fitbit/sensor.py, use _LOGGER to debug

_LOGGER.info("fetch_access_token from data: %s", data.get('code'))
_LOGGER.info("redirect url: %s", redirect_uri)

The new manifest pull fixed it for me. Side note, something weird about the cache retaining an older api ID in Chrome, make sure it's correct also.

Thank you an-pham for showing me how to narrow down my logging results. I was able to see that my manifest wasn't parsing so I fixed my syntax and voila...I'm up and running with the suggestion from SPCulhane. This is why I love this community.

I actually moved my entire setup over to Hass.io and am still struggling with this :) all I keep getting is an InvalidClientId error:

2019-05-25 01:46:58 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/real_ip.py", line 33, in real_ip_middleware
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/ban.py", line 68, in ban_middleware
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/auth.py", line 216, in auth_middleware
    return await handler(request)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/http/view.py", line 112, in handle
    result = handler(request, **request.match_info)
  File "/config/custom_components/fitbit/sensor.py", line 324, in get
    redirect_uri)
  File "/usr/local/lib/python3.7/site-packages/fitbit/api.py", line 146, in fetch_access_token
    code=code)
  File "/config/deps/lib/python3.7/site-packages/requests_oauthlib/oauth2_session.py", line 244, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 411, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 379, in parse_token_response
    validate_token_parameters(params)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 386, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/config/deps/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 415, in raise_from_error
    raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.InvalidClientIdError: (invalid_request) Invalid client_id parameter value.

I tried adding the manifest.json file, editing it as suggested, but doesn't seem to make any difference. As soon as I hit the last step with the callback function, it fails. I should add my instance is not HTTPS, so I have to remove the 's' from the callback URL and reload, but I feel like that shouldn't be causing it to fail. Any ideas?

EDIT: Sorry about the lack of line breaks in the log, not sure why they aren't coming through.

I still have the same problem with fitbit. I get the error 500 server.
I'm trying @SPCulhane trick now.

Still getting the 500 error issue with Hass on the Fitbit callback with Hass ver 0.108.5. Doesn't seem to be fixed yet.

I'm also getting the 500 error on 108.9.

Still issues on Home Assistant 0.110.1
Please authorize Fitbit by visiting http://192.168.0.63:8123/api/fitbit

Fitbit
The app you're trying to connect did not provide valid information to Fitbit. Please report this issue to them.

Developer information: invalid_request - Invalid redirect_uri parameter value

Could this issue be further compounded with the

  external_url: https://xxxxxxxxxx.duckdns.org
  internal_url:

This integration was working prior to the 0.110.0 release however I did do a NGINX update and thats when I noticed my sensors had gone.

It looks like the Fitbit component is generating it's own URL and sending that, rather than using the external_url which has been defined ...

Was this page helpful?
0 / 5 - 0 ratings