Managing Data Sources

The Fitness REST API lets you create, obtain, and update data sources. A data source represents a unique source of sensor data. You use data sources to insert fitness data into the fitness store, and you can retrieve fitness data inserted by a particular data source.

Data sources are represented by the Users.dataSources resource.

Create a data source

This example demonstrates how to create a new data source named "MyDataSource" that provides step count increments.

HTTP method

POST

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataSources

Request body
 {
  "dataStreamName": "MyDataSource",
  "type": "derived",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "Foo Example App",
    "version": "1"
  },
  "dataType": {
    "field": [
      {
        "name": "steps",
        "format": "integer"
      }
    ],
    "name": "com.google.step_count.delta"
  },
  "device": {
    "manufacturer": "Example Manufacturer",
    "model": "ExampleTablet",
    "type": "tablet",
    "uid": "1000001",
    "version": "1.0"
  }
}

Response

If the data source is created successfully, the response is a 200 OK status code. The response body contains a JSON representation of the data source, including a datasource.dataStreamId property that you can use as the data source ID for subsequent requests.

Curl command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X POST \
  --header "Content-Type: application/json;encoding=utf-8" -d @createds.json \
  "https://www.googleapis.com/fitness/v1/users/me/dataSources"

Get a particular data source

This example demonstrates how to retrieve the data source ("MyDataSource") that you created in the previous example. When you create a new data source, the dataStreamId includes a unique identifier (shown as "1234567890" in these examples). This is your developer project number, and it will be the same for all requests made using that particular developer account. Be sure to use the dataStreamId from the data source you created.

HTTP method

GET

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource

Request body

None

Response

If the data source exists, the response is a 200 OK status code. The response body contains a JSON representation of the data source.

Curl command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X GET \
  --header "Content-Type: application/json;encoding=utf-8" \
  "https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource"

Get aggregated data

This example demonstrates how to query a specific data source for aggregated data, in this case estimated_steps, which is the data source used to show step count in the Google Fit app. Note that timestamps in the JSON request body are in milliseconds.

HTTP method

POST

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

Request body
 {
  "aggregateBy": [{
    "dataSourceId":
      "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],
  "bucketByTime": { "durationMillis": 86400000 },
  "startTimeMillis": 1454284800000,
  "endTimeMillis": 1455062400000
}

Response

If the data source(s) exist, the response is a 200 OK status code. The response body contains a JSON representation of the data source.

Curl command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X POST \
  --header "Content-Type: application/json;encoding=utf-8" -d @aggregate.json \
  "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"

Update a data source

This example demonstrates how to update the name and device version for a data source.

HTTP method

PUT

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource

Request body
 {
  "dataStreamId": "derived:com.google.step_count.delta:1234567890:Example Manufacturer:ExampleTablet:1000001:MyDataSource",
  "dataStreamName": "MyDataSource",
  "type": "derived",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "Foo Example App",
    "version": "1"
  },
  "dataType": {
    "field": [
      {
        "name": "steps",
        "format": "integer"
      }
    ],
    "name": "com.google.step_count.delta"
  },
  "device": {
    "manufacturer": "Example Manufacturer",
    "model": "ExampleTablet",
    "type": "tablet",
    "uid": "1000001",
    "version": "2.0"
  }
}

Response

If the data source is updated successfully, the response is a 200 OK status code. The response body contains a JSON representation of the data source.

Curl command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X PUT \
  --header "Content-Type: application/json;encoding=utf-8" -d @updateds.json \
  "https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource"

Delete a data source

This example demonstrates how to delete a data source.

HTTP method

DELETE

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource

Request body

None

Response

If the data source is deleted successfully, the response is a 200 OK status code. The response body contains a JSON representation of the data source that was deleted.

Curl command

$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X DELETE \
  --header "Content-Type: application/json;encoding=utf-8" \
  "https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource"

发送以下问题的反馈:

此网页
Google Fit
Google Fit