如何通过Google Fit REST API编程获取可导出的健身活动数据
Hey there! Since you’ve already got your activity sessions list via the Sessions List API, let’s walk through how to pull the specific exportable data for a single session to integrate with services like Strava.
Step 1: Grab the Session ID from Your Existing List
First, you’ll need the unique identifier for the session you want to export. Your sample session object only shows modifiedTimeMillis, but each entry in the session array will also have a sessionId field (a string like 1234567890123456789). That’s your key to target the exact activity you care about.
Step 2: Get Detailed Session Metadata with Sessions:get API
Call the Sessions:get endpoint to retrieve full details about the session, including its time range and linked data sources. Here’s how to structure the request:
- Method:
GET - URL:
https://www.googleapis.com/fitness/v1/users/me/sessions/{sessionId} - Headers: Include your OAuth 2.0 access token:
Authorization: Bearer YOUR_ACCESS_TOKEN
This response will give you critical fields you’ll need next:
startTimeMillisandendTimeMillis(to define the bounds of your data request)dataSourceIds(an array of IDs pointing to where the raw activity data is stored)
Step 3: Fetch Raw Activity Data with DataSources.datasets.get
Use each dataSourceId from the previous step to pull the actual raw datapoints. For every data source ID, call the Users.dataSources.datasets.get endpoint:
- Method:
GET - URL:
https://www.googleapis.com/fitness/v1/users/me/dataSources/{dataSourceId}/datasets/{startTimeMillis}-{endTimeMillis} - Headers: Same
Authorizationheader as before
The response will include a point array with raw data entries. For example, a running session might return:
- Location datapoints (latitude/longitude pairs)
- Speed, pace, or heart rate values
- Cumulative distance metrics
Note on Pagination
If your session has a lot of data (like a long run with frequent GPS points), the response might include a nextPageToken. Just append ?pageToken=TOKEN to the URL to fetch the remaining datapoints until there’s no more token.
Step 4: Convert Raw Data to Exportable Formats (e.g., .fit, GPX)
Google Fit doesn’t return pre-formatted .fit or GPX files directly, but you can convert the raw datapoints using open-source libraries:
- For .fit files: Use a JavaScript library like
fit-file-parserto map your datapoints into the .fit format schema (both parsing and building are supported). - For GPX: Use tools like
gpx-builderto convert location datapoints into valid GPX tracks—Strava supports GPX imports natively.
Example Snippet (GPX Conversion Concept)
If you have location datapoints, here’s a quick pseudocode example to build a GPX track:
// Pseudocode for building GPX from location points const gpxBuilder = new GPXBuilder(); const track = gpxBuilder.createTrack(); const segment = track.createSegment(); locationPoints.forEach(point => { segment.createPoint({ lat: point.value[0], lon: point.value[1], time: new Date(parseInt(point.startTimeMillis)) }); }); const gpxString = gpxBuilder.toString();
Key Permissions Reminder
Make sure your OAuth 2.0 scope includes the necessary read access. For activity data, you’ll need https://www.googleapis.com/auth/fitness.activity.read (use write instead if you plan to modify data later).
内容的提问来源于stack exchange,提问作者Sankalp Sharma




