FitBit unable to authorize

I moved over to HAOS last week and I cannot get it to authenticate at all now and it throws a 500 error after I allow all the available sensors. I deleted my old API setup at Fitbit and started a new one but it is still the same. The logs show this error -

Detected I/O inside the event loop. This is causing stability issues. Please report issue for fitbit doing I/O at homeassistant/components/fitbit/sensor.py, line 362: result = self.oauth.fetch_access_token(data.get("code"), redirect_uri)

Someone has logged a new issue here

There is a now a fix for this by modifying the Fitbit sensor.py file. If you add the 3 files as a custom component and restart HA the authentication should not go through.

Further help and a link to the files are in my GitHub repository here

Thanks again to @Yusi for supplying the fix.

1 Like

Thank you for this! Had given up on Fitbit in HA

A slight change since you posted it

http:
  base_url:

has been deprecated and after adding my DNS url to Settings > General > External / Internal URL (https://home.assistant) and restarting I was able to setup the integration

If it fails take a look at the URL


What worked for me is to open the new url in another browser (Firefox)

Glad you got it working!

hello
y have problems to autorize. do you know if the problem comes again?

Yes you can use this integration without exposing HA to the internet - I just got it working by enabling HTTPS using this guide I found on the Github issue tracker: https://gist.github.com/ammgws/d3fb6d197e6036e496d4fee17e22a84f

2 Likes

Can this work for 2 fitbit account?

THANK YOU to everyone in this thread and the few others on the topic. I feel like it more or less works as instructed on the integration page, but my gosh is it absolutely underexplained and without much needed details. I’d almost rather redo all my Tuya before needing to figure this one out again…

Help someone else out. Write up your experience and post it here. I have a PV6, but haven’t the skills to set this up.

1 Like

This has been one of the most frustrating and under-explained integrations I have installed. The official instructions at home-assistant.io/integrations/fitbit lack a lot of detail and I’ve had problems with the sensor install, authorisation and 500 server errors.

Here is what I have found to work for me:

Homeassistant running on Raspberry Pi 4 Model B
Home Assistant OS 6.6
core-2021.11.4
supervisor-2021.10.8

Fitbit needs a secure URL (https) for the callback url. I’m using my nabu.casa address for this. in Homeassistant, go to Configuration->General and scroll down to External/Internal URL box. Enter your external URL in BOTH boxes.

External URL: https://xxxx.ui.nabu.casa

Internal URL: https://xxxx.ui.nabu.casa

Next go to github.com/BertrumUK/fitbit
and download the 3 files: init.py manifest.json sensor.py

In Homeassistant, open your file editor and in /config/custom_components/ create a new folder called fitbit. Load the 3 files to this folder.

Back in the config folder, open configuration.yaml and add your fitbit sensors you want to track and save the file:

sensor:
  - platform: fitbit
    clock_format: 12H
    unit_system: metric
    monitored_resources:
      - "body/weight"
      - "devices/battery"
      - "activities/heart"
      - "activities/steps"

Restart Homeassistant.

Once you have restarted, under Notifications in the side menu, a new notification will have appeared. Click on it and then click on configure. This will give you the parameters required to configure your Fitbit app.

Go to dev.fitbit.com/apps/new to create a new app on Fitbit for your integration. (If you do not have a fitbit dev account you will need to create one)

Fill in the form on the screen. Most boxes are required so I used my nabu.casa address for them all.

The important one is the callback URL: https://xxxx.ui.nabu.casa:8123/api/fitbit/callback

Note, Homeassistant shows a trailing “/” but I found this caused me an error, so I left it off and it worked fine. Be sure to pick “Personal” for the OAuth type.

Take note of the OAuth 2.0 Client ID and Client Secret as you will need these in Homeassistant.

Return to Homeassistant and open the file fitbit.conf in the configuration folder. Note, you still have the fitbit configuration box open at this point, so I got around this by opening a second instance of my HA and editing the file there. Fill in the Client ID and Client Secret that Fitbit provided in the app and save the file.

Then click the blue “I have Saved My Client ID and Secret Info” button.

Click on Configure again and Homeassistant will prompt you to authorise the Fitbit app with Homeassistant

Here is where I had issues with the “500 server error. Server got itself in trouble”

DO NOT click on the link, instead copy it, and replace your casa address with your internal address

Change

https://xxx.ui.nabu.casa/api/fitbit
to

http://192.168.xxx.xx:8123/api/fitbit

Post this to a new browser window

Fitbit will respond with an error: unauthorised client in url

Copy the full URL in the browser window:

Replace the client_id and redirect_url

client_id is the one you entered to fitbit.confg

redirect_url is your casa address

OLD URL

https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=23XCDQ&redirect_uri=http://192.168.xxx.xxx:8123/api/fitbit/callback&scope=activity+heartrate+nutrition+profile+settings+sleep+weight&state=Zmo69Wkex96S3QHIL70xcpyqAA3PebPi

NEW URL

https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=[ENTER CLIENT_ID HERE]&redirect_uri=https://xxx.ui.nabu.casa/api/fitbit/callback&scope=activity+heartrate+nutrition+profile+settings+sleep+weight&state=Zmo69Wkex96S3QHIL70xcqAA3PebPi

Copy and paste this new URL in a browser window

The Fitbit authorisation should appear. Select “Allow All” and click on the “Allow” button. A small file will download, ignore it, it just contains a text string “Fitbit has been successfully authorized! You can close this window now!”

Return to Homeassistant and the configuration box should have vanished and you should have sensors from Fitbit in your entities list, e.g. sensor.weight, sensor.steps, etc.

6 Likes

My experience was slightly different. As I was on my internal domain when I started, when I came to auth it tried to use the internal IP address. When the fitbit auth error appeared, I just replaced the IP with the https Casa address.

Great tutorial :slight_smile:

1 Like

Hi Brian,
Yes, I think I did that the first time I installed the integration. I’ve installed it twice on 2 different HA setups. The second was to document what I did, both for here, and also so I had a record of how I did it if I ever had to do a reinstall. I used the external URL as the Fitbit app requires an HTTPS address to set up the app.

1 Like

Just in case it was missed earlier, you can actually set it up without using an external URL if you do something like this: https://gist.github.com/ammgws/d3fb6d197e6036e496d4fee17e22a84f

Of course if you already have an external URL then I guess it doesn’t matter, but just putting it out there again for those of us who want to keep their HA instance local only.

I think the main two main issues with this integration are:

  1. Clunky setup process

  2. Fitbit changing their API to enforce HTTPS as of a couple of years ago

The first issue should be solved if someone is willing to do the work mentioned here: Clarify that only HTTPS can be used in fitbit by ammgws · Pull Request #57116 · home-assistant/core · GitHub

The second issue should hopefully be solved once a new release of HA is made that has this PR included: Clarify that only HTTPS can be used in fitbit by ammgws · Pull Request #57116 · home-assistant/core · GitHub

Hello @Shortland

I followed your steps but the step below does not work:

I do not get a notification thus I can’t go to next step. Any idea?

UPDATE-------
The problem was that you absolutely need to have filled the external and internal URL in your Configuration. Otherwise the notification won’t appear. Once I did that, I could follow up on your tutorial and I managed to link some Fitbit sensors in my HA. I know it was mentionned by @Shortland, I just thought I had already done that because I am using an external DuckDNS link for my HA.
Capture d’écran, le 2022-03-04 à 09.38.55

Mant thanks for this manual. It works for me.

Does anyone know if it is possible to add a second fitbit to home assistant?

Thanks.
Koen

Where is this CONFIGURATION?
I can’t find any Configuration in my HAOS installation excelt the yaml file and it doesn’t look at all like this.
So basically even though I have it setup to work with HTTPS I don’t know where to tell it, and the Fitbit script says it can’t find it.

Go to Parameters → System → Network

Capture d’écran, le 2023-07-05 à 06.39.22
Capture d’écran, le 2023-07-05 à 06.39.36
Capture d’écran, le 2023-07-05 à 06.39.46

Thank you Vincent,
My system (latest HAOS) is different so I have no Parameters but I was able to find System/Network so I think I got it.
Thanks again.

It is no longer necessary to load the modified component in “custom_components”. The official component is already modified to work as of 2023-07.

In my case I don’t use nabu.casa I use Duck DNS so some steps are different

Once you are all set with Duck DNS (in case you just configured it) it is recommended to restart HA.

  • Next is to update the HA internal and external URL
    Settings-> System-> Network → (Home Assistant URL)

Internet: https://CustomName.duckdns.org:8123
Local Network https://192.168.x.xxx:8123

Changing for the values corresponding to your configuration.

Once I do this, restart HA

  • Creating the Fitbit Dev App
    If you have already created it, you can edit the values if necessary

The important one is the callback url:

https://xxxx.ui.nabu.casa:8123/api/fitbit/callback
Or
https://CustomName.duckdns.org:8123/api/fitbit/callback

Note that Homeassistant displays a trailing “/” but as Peter Shortland discovered this causes an error. This return URL is without “/” at the end. Be sure to choose “Personal” for the OAuth type.

The values of “OAuth 2.0 Client ID” and “Client Secret” will be needed for the Fitbit startup process to populate the “/config/fitbit.conf” file.

  • Once we have this we can add the sensor to HA
# Example configuration.yaml entry
sensor:
  - platform: fitbit
    clock_format: 12H
    monitored_resources:
      - "body/weight"

Copied from the original from the official Fitbit settings page

  • We restart HA

  • It will ask us to enter the data of “/config/fitbit.conf”

  • As a precaution we restart HA again

  • Grant Fitbit permissions

Then comes the critical process of giving the Fitbit app permissions to HA.
We will get a tab like “URL/api/Fitbit”
important not to click the link, copy and edit it
We change the URL to have the configuration of the internal network
https://192.168.x.xxx:8123/api/Fitbit
We enter the tab and grant all the permissions we need
we give to send

and everything works! :smiley:

You can now see the new sensors defined in the entities

2 Likes