管理数据源

利用 Fitness REST API,您可以创建、获取和更新数据源。数据源表示传感器数据的唯一来源。您可以使用数据源将健身数据插入健身存储区,并且可以检索由特定数据源插入的健身数据。

数据源由 Users.dataSources 资源表示。

创建数据源

此示例演示了如何创建名为“MyDataSource”并提供步数增量的新数据源。

HTTP 方法
发布
请求网址
https://www.googleapis.com/fitness/v1/users/me/dataSources
请求正文
{
  "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"
  }
}

响应

如果成功创建数据源,响应将是 200 OK 状态代码。响应正文包含数据源的 JSON 表示法,其中包括可用作后续请求的数据源 ID 的 datasource.dataStreamId 属性。

卷曲命令
$ 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"

获取特定数据源

此示例演示了如何检索在上一个示例中创建的数据源(“MyDataSource”)。创建新的数据源时,dataStreamId 会包含一个唯一标识符(在这些示例中显示为“1234567890”)。这是您的开发者项目编号,使用该特定开发者账号发出的所有请求都是相同的。请务必使用您创建的数据源中的 dataStreamId

HTTP 方法
获取
请求网址
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
请求正文
响应
如果数据源存在,则响应为 200 OK 状态代码。响应正文包含数据源的 JSON 表示法。
卷曲命令
$ 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"

获取汇总数据

此示例演示了如何查询特定数据源中的汇总数据,在本例中为 estimated_steps(数据源用于在 Google 健身应用中显示步数)。请注意,JSON 请求正文中的时间戳以毫秒为单位。

HTTP 方法
发布
请求网址
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
请求正文
{
  "aggregateBy": [{
    "dataSourceId":
      "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],
  "bucketByTime": { "durationMillis": 86400000 },
  "startTimeMillis": 1454284800000,
  "endTimeMillis": 1455062400000
}

响应

如果存在数据源,响应将是 200 OK 状态代码。响应正文包含数据源的 JSON 表示法。

卷曲命令
$ 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"

更新数据源

此示例演示了如何更新数据源的名称和设备版本。

HTTP 方法
PUT
请求网址
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
请求正文
{
  "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"
  }
}

响应

如果数据源已成功更新,响应将是 200 OK 状态代码。响应正文包含数据源的 JSON 表示法。

卷曲命令
$ 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"

删除数据源

此示例演示了如何删除数据源。

HTTP 方法
删除
请求网址
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
请求正文
响应
如果成功删除数据源,响应将是 200 OK 状态代码。响应正文包含已删除数据源的 JSON 表示法。
卷曲命令
$ 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"