orcasgit / python-fitbit

Fitbit API Python Client Implementation
Other
621 stars 329 forks source link

Error trying to get heart data #151

Open thegabriele97 opened 5 years ago

thegabriele97 commented 5 years ago

Hi,

I am developing a python application for my university and I need to access heart rate during an activity (avg data or something that tell me how user is performing during a run). I am using this api https://github.com/orcasgit/python-fitbit .

I have this code

fitbit_api.py

import fitbitPackage.fitbit
import fitbitPackage.gather_keys_oauth2 as Oauth2
import datetime

class FitbiAapi:
    __client_id__ = None
    __client_secret__ = None
    __server__ = None
    __access_token__ = None
    __refresh_token__ = None
    __auth_client__ = None

    def __init__(self, client_id, client_secret):
        self.__client_id__ = client_id
        self.__client_secret__ = client_secret
        self.__server__ = Oauth2.OAuth2Server(self.__client_id__, self.__client_secret__)

    def get_authorize_url(self):
        return self.__server__.get_authorize_url()

    def start_response_poll(self):
        self.__server__.start_response_poll()
        self.__access_token__ = str(self.__server__.fitbit.client.session.token['access_token'])
        self.__refresh_token__ = str(self.__server__.fitbit.client.session.token['refresh_token'])
        self.__auth_client__ = fitbitPackage.fitbit.Fitbit(self.__client_id__, self.__client_secret__, oauth2=True, access_token=self.__access_token__, refresh_token=self.__refresh_token__)

    def get_auth_client(self) -> fitbitPackage.fitbit.Fitbit:
        return self.__auth_client__

def get_right_dateFormat(offset: int = 0) -> str:
    return str((datetime.datetime.now() - datetime.timedelta(days=offset)).strftime("%Y-%m-%d"))

main.py

from fitbitPackage import fitbit_api, get_right_dateFormat

print(fitbit_api.get_authorize_url())
fitbit_api.start_response_poll()

client = fitbit_api.get_auth_client()

today_date = get_right_dateFormat()
yesterday_date = get_right_dateFormat(-1)

fitbit_stats = client._COLLECTION_RESOURCE('heart', date=today_date)
print (fitbit_stats)

but when I execute this code, i obtain this error:

Traceback (most recent call last):
  File "main_testFitBit.py", line 11, in <module>
    fitbit_stats = client._COLLECTION_RESOURCE('heart', date=today_date)
  File "C:\Users\gabri\OneDrive\Desktop\AmIRunning-code\src\fitbitPackage\fitbit\api.py", line 348, in _COLLECTION_RESOURCE
    return self.make_request(url, data)
  File "C:\Users\gabri\OneDrive\Desktop\AmIRunning-code\src\fitbitPackage\fitbit\api.py", line 256, in make_request
    response = self.client.make_request(*args, **kwargs)
  File "C:\Users\gabri\OneDrive\Desktop\AmIRunning-code\src\fitbitPackage\fitbit\api.py", line 99, in make_request
    exceptions.detect_and_raise_error(response)
  File "C:\Users\gabri\OneDrive\Desktop\AmIRunning-code\src\fitbitPackage\fitbit\exceptions.py", line 86, in detect_and_raise_error
    raise HTTPNotFound(response)

fitbitPackage.fitbit.exceptions.HTTPNotFound: The API you are requesting could not be found.

Why?

Etherealflux commented 5 years ago

Same thing here. I would guess that something changed on Fitbit's end. I'll have a closer look at it soon.

thegabriele97 commented 5 years ago

I solved using another functions. You can look at my project (AmIRunning) on my profile

freshwuzhere commented 5 years ago

Hi @Etherealflux ,

Any chance to look at this? I am about to implement Fitbit API call but would love to use this feature of your library!

Thanks

pepijndevos commented 4 years ago

So save the next person the trouble, the code @thegabriele97 seems to be referring to is

fit_heart = client.intraday_time_series('activities/heart', base_date=today_date, detail_level='1sec')['activities-heart-intraday']['dataset']

A fix for this would be very much appreciated.