c99koder / personal-influxdb

Import data from various APIs into InfluxDB
Apache License 2.0
263 stars 36 forks source link

fitbit gets unauthorized after 1 day #5

Closed shrippen closed 3 years ago

shrippen commented 3 years ago

Hello,

I have another problem. After your help I got the fitbit script to work. But after about a day (don't know for sure, logs got lost) I get the error message "HTTP request failed: 401 Client Error: Unauthorized for url:"

I can fix this by generating a new access token but I don't want to do this manually every day. There is an option in the fitbit api workflow page to set the duration for the token in ms and I've already set it to ridicously high. Do you have a recommendation?

c99koder commented 3 years ago

The script is storing the refresh token in a file named .fitbit-refreshtoken in the folder you're running the script from. This token is used to automatically request a new token when when you run the script. It's possible this filename isn't valid on Windows, you might need to search/replace the filename in the script

shrippen commented 3 years ago

So, I tried various things but so far I haven't been able to get this working. Just out of curiosity did you choose an Implicit Grant Flow or the Authorization Code Flow? The seem to differ.

c99koder commented 3 years ago

You need to use authorization code flow, and the code should only be used once. After it's authenticated, it should be saving the refresh token in .fitbit-refreshtoken which will be used to request a new token each time you run the script. You might need to change the filename or add a path to it if your platform/environment isn't writing to the correct location.

shrippen commented 3 years ago

Sorry to bother again. I am not able to make this work. I have now migrated the scripts to a debian host. I have also tried to provide absolute paths for .fitbit-refreshtoken inside the script - it didn't help and I don't know how to go further. I have tested with a sample script provided by this site: https://www.golinuxcloud.com/python-write-to-file/#Example_1_Writing_to_an_empty_file to check if python3 has the ability to write in this folder under this user and can confirm it does.

Is there any way to diagnose this problem any further?

c99koder commented 3 years ago

Just to confirm, after the initial login you're not setting anything for FITBIT_ACCESS_TOKEN or FITBIT_INITIAL_CODE in the script? Those should be empty strings in order for it to use the refresh token stored in the file. You can try adding some extra print statements around lines 207 and 230 to make sure the token is being loaded, updated, and saved correctly.

shrippen commented 3 years ago

Okay, I have added print statements at those locations. Let's what it does. But just to be sure I understood correctly. The strings for the Accees token and Initial Code remain empty? So I just provide the client id and secret. I thought I should generate the token and code using the OAuth2 Tutorial Page and fill these two strings (those variables at the beginning of the script). I'm confused.