管理数据源

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 格式的数据源,包括一个 datasource.dataStreamId 属性,您可以将其用作后续请求的数据源 ID。

网址命令
$ 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 方法
输入
请求网址
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"