Hey Team, I struggling to access aggregate data from the fitness API
require 'google/apis/fitness_v1'
Fitness = Google::Apis::FitnessV1
fitness = Fitness::FitnessService.new
fitness.authorization = user.token
fitness.list_user_data_sources('me')
Sending HTTP get https://www.googleapis.com/fitness/v1/users/me/dataSources?
200
However when I try and access daily steps I get a 500 error
daily_steps = fitness.aggregate_dataset('me', 'com.google.activity.summary')
Sending HTTP post https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate?
500
...
Caught error Server error
Error - #<Google::Apis::ServerError: Server error>
Google::Apis::ServerError: Server error
I assume this is a server side issue but am I doing something wrong in my request? I think I'm missing something and haven't being able to find reliable examples on aggregate fitness requests. Any direction would be appreciated. Cheers Team!
I haven't tracked this down yet (I too suspect a server side issue), but wanted to link these issues: google/google-api-nodejs-client#1176
@gregology - I've just spent this evening trying to figure this out myself, and have come up with the following (which will give you the steps for today):
aggregate_by = Google::Apis::FitnessV1::AggregateBy.new
aggregate_by.data_type_name = 'com.google.step_count.delta'
aggregate_by.data_source_id = 'derived:com.google.step_count.delta:com.google.android.gms:estimated_steps'
bucket_by_time = Google::Apis::FitnessV1::BucketByTime.new
bucket_by_time.duration_millis = 86400000
aggregate_request = Google::Apis::FitnessV1::AggregateRequest.new
aggregate_request.aggregate_by = [aggregate_by]
aggregate_request.bucket_by_time = bucket_by_time
aggregate_request.start_time_millis = (Time.new(Time.now.year, Time.now.month, Time.now.day, 0, 0, 0).to_f * 1000).to_i
aggregate_request.end_time_millis = (Time.new(Time.now.year, Time.now.month, Time.now.day, 23, 59, 0).to_f * 1000).to_i
daily_step = fitness.aggregate_dataset('me', aggregate_request)
I converted this from the JSON given in the REST example on this page (and lots of reading between the lines of the Ruby documentation):
https://developers.google.com/fit/scenarios/read-daily-step-total
Thank you @jamgregory, that worked perfectly and fixes my problem. I'll keep this issue open because the server shouldn't be throwing 500 errors
No worries @gregology, glad to be able to help. I think the 500 errors are server side issues - I've been trying to extract other data with no useful error messages aside from error 500 to indicate I'm doing something wrong
Hi,
So yes, you do need to send an appropriate AggregateRequest object, as detailed in the documentation. Note that we have a new documentation site for this gem at: https://googleapis.dev/ruby/google-api-client/latest that the Google client libraries team maintains, so feel free to use that if rubydoc.info isn't working.
There isn't much that the client library can do about backend issues. If you're so inclined, you can report the 500 at the fitness support page. Go to https://developers.google.com/fit/support and click Send Feedback in the upper right.