pkpio/fitbit-googlefit

Error on adding points to a dataset: Invalid arguments

jensb89 opened this issue · 3 comments

Somehow the app can create a new dataset, but is unable to fill it with points (patch request).

The following error occurred:

  File "app.py", line 112, in <module>
    main()
  File "app.py", line 77, in main
    remote.SyncFitbitToGoogleFit('steps',date_stamp)
  File "/home/pi/Fitbit/fitbit-googlefit/remote.py", line 152, in SyncFitbitToGoogleFit
    return self.SyncFitbitIntradayToGoogleFit(dataType, date_stamp)
  File "/home/pi/Fitbit/fitbit-googlefit/remote.py", line 195, in SyncFitbitIntradayToGoogleFit
    self.WriteToGoogleFit(dataSourceId, googlePoints)
  File "/home/pi/Fitbit/fitbit-googlefit/remote.py", line 96, in WriteToGoogleFit
    point=data_points)
  File "/home/pi/Fitbit/fitbit-googlefit/fitbitenv/lib/python3.5/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/pi/Fitbit/fitbit-googlefit/fitbitenv/lib/python3.5/site-packages/googleapiclient/http.py", line 907, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/fitness/v1/users/me/dataSources/raw%3Acom.google.step_count.delta%3A839740875803%3Afitbit%3Acharge4%3Aio.pkp.fbit-gfit/datasets/1587765600000000000-1587851940000000110?alt=json returned "Request contains an invalid argument.">

Any ideas why?

The documentation says: " Points: A partial list of data points contained in the dataset, ordered by largest endTimeNanos first." When I send the data points in reverse order the error is gone and the script says it has synced 1400 points. Still, I can't see them on the Google Fit iOs App. Only Activities are shown there. Steps and everything else is missing :(

Gave it one more try by sending just the first value of the array. Again, the request went through, but no data saved.

Here the input data:
{'minStartTimeNs': 1587938400000000000, 'maxEndTimeNs': 1587938460000000000, 'dataSourceId': 'raw:com.google.step_count.delta:839740875803:fitbit:charge4:io.pkp.fbit-gfit', 'point': [{'value': [{'intVal': 0}], 'startTimeNanos': 1587938400000000000, 'endTimeNanos': 1587938460000000000, 'dataTypeName': 'com.google.step_count.delta'}]}

And here the response of the patch request:

{
    "dataSourceId": "raw:com.google.step_count.delta:839740875803:fitbit:charge4:io.pkp.fbit-gfit",
    "maxEndTimeNs": "1587938460000000000",
    "minStartTimeNs": "1587938400000000000",
    "point": []
}

... I can't see anything wrong. I have all the necessary rights (all scopes activated) and there is also no auth error, the point is just not saved.

Might be even an error with Googles API? Is anybody still using this app and is it still working??

Ok, here is a solution. Change the WriteToGoogleFit() function:

with open('auth/google.json', 'r') as f:
                                        auth = json.load(f)
                                        token = auth['access_token']
                                        url = 'https://www.googleapis.com/fitness/v1/users/me/dataSources/'+dataSourceId+'/datasets/'+datasetId
                                        head = {"Authorization":"Bearer "+token,"Content-Type": "application/json;encoding=utf-8"}
                                        payload = dict(dataSourceId=dataSourceId,
                                               maxEndTimeNs=maxLogNs,
                                               minStartTimeNs=minLogNs,
                                               point=data_points)
                                        response = requests.patch(url, json.dumps(payload), headers=head)

Somehow, it seems the googleApiClient caused the problems.
It may be that I accidentally updated this to a newer version when the authentication did not work at the beginning. So I don't know if the above code or the reverse order is really necessary.
Will leave it here, in case the problem occurs to someone else :)