Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dipcb05's avatar

Google Fit API dataset extraction

I have extracted google fit data sources. I have read the documentation, I need to get this format to get a response dataset of each data source from the rest API - https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001 /datasets/1397513334728708316-1397515179728708316

This is the stream id I tried - dataStreamId: "derived:com.google.active_minutes:com.google.android.fit:Xiaomi:Redmi 7A:220c08bd:top_level"

I made a loop to extract all datasets of every data source for the user.

this is my code -

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Google_Client;
use Google_Service_Fitness;

class Health extends Controller
{
    public function fetch_health_data()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        $user_session = $user->users_sessions->last();

        $client = new Google_Client();
        $client->setApplicationName('Fitness API');
        $client->setAccessType('offline');
        $client->setAccessToken($user_session->access_token);
        $fitnessService = new Google_Service_Fitness($client);
        $dataSources = $fitnessService->users_dataSources->listUsersDataSources("me");
        $healthData = [];

        foreach ($dataSources as $dataSource) {
            $dataType = $dataSource->dataType->name;
            $dataStreamId = $dataSource->dataStreamId;
            dd($dataSource);
            $dataSets = $fitnessService->users_dataSources_datasets->get("me", "me", $dataStreamId);
            dd($dataSets);
            foreach ($dataSets->getPoint() as $dataPoint) {
                $startTimeMillis = $dataPoint->getStartTimeNanos() / 1000000;
                $endTimeMillis = $dataPoint->getEndTimeNanos() / 1000000;

                $healthData[] = [
                    'user_id' => $user_id,
                    'data_type' => $dataType,
                    'start_time' => date('Y-m-d H:i:s', $startTimeMillis / 1000),
                    'end_time' => date('Y-m-d H:i:s', $endTimeMillis / 1000),
                    'value' => $dataPoint->getValue(),
                ];
            }
        }
        dd($healthData);
        return $healthData;
    }
}

when i try to get data from $datasets, i am getting this error - { "error": { "code": 400, "message": "Invalid datasetId: derived:com.google.active_minutes:com.google.android.fit:Xiaomi:Redmi 7A:220c08bd:top_level", "errors": [ { "message": "Invalid datasetId: derived:com.google.active_minutes:com.google.android.fit:Xiaomi:Redmi 7A:220c08bd:top_level", "domain": "global", "reason": "invalidArgument" } ], "status": "INVALID_ARGUMENT" } }

0 likes
1 reply

Please or to participate in this conversation.