管理資料來源

Fitness REST API 可讓您建立、取得及更新資料來源。資料來源代表感應器感應器的唯一來源。你可以使用資料來源將健身資料插入健身儲存庫,並擷取特定資料來源插入的健身資料。

資料來源以 Users.dataSources 資源表示。

建立資料來源

以下範例說明如何建立新的名為「quoData;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 指令
$ 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 包含專屬 ID (在以下範例中會顯示為「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 指令
$ 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 Fit 應用程式中顯示步數計數的資料來源。請注意,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 指令
$ 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 指令
$ 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 指令
$ 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"