API REST của Fitness cho phép bạn tạo, lấy và cập nhật nguồn dữ liệu. Một nguồn dữ liệu đại diện cho một nguồn dữ liệu cảm biến duy nhất. Bạn sử dụng các nguồn dữ liệu để chèn dữ liệu về hoạt động thể dục vào kho dữ liệu thể dục và bạn có thể truy xuất dữ liệu về hoạt động thể dục do một nguồn dữ liệu cụ thể chèn.
Các nguồn dữ liệu được biểu thị bằng tài nguyên Users.dataSources
.
Tạo nguồn dữ liệu
Ví dụ này minh hoạ cách tạo nguồn dữ liệu mới có tên "MyDataSource" cung cấp số lượng bước tăng lên.
- Phương thức HTTP
- ĐĂNG
- URL yêu cầu
https://www.googleapis.com/fitness/v1/users/me/dataSources
- Nội dung yêu cầu
{ "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" } }
- Phản hồi
Nếu nguồn dữ liệu được tạo thành công thì phản hồi là mã trạng thái
200 OK
. Phần nội dung phản hồi chứa bản trình bày JSON của nguồn dữ liệu, bao gồm thuộc tínhdatasource.dataStreamId
mà bạn có thể sử dụng làm mã nhận dạng nguồn dữ liệu cho các yêu cầu tiếp theo.- Lệnh 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"
Nhận một nguồn dữ liệu cụ thể
Ví dụ này minh họa cách truy xuất nguồn dữ liệu ("MyDataSource")
mà bạn đã tạo trong ví dụ trước. Khi bạn tạo một nguồn dữ liệu mới,
dataStreamId
sẽ bao gồm một giá trị nhận dạng duy nhất (được hiển thị dưới dạng "1234567890" trong những
ví dụ này). Đây là số dự án dành cho nhà phát triển của bạn và sẽ giống nhau cho tất cả các yêu cầu được đưa ra bằng tài khoản nhà phát triển cụ thể đó. Hãy nhớ sử dụng
dataStreamId
từ nguồn dữ liệu bạn đã tạo.
- Phương thức HTTP
- TẢI
- URL yêu cầu
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
- Nội dung yêu cầu
- Không có
- Phản hồi
- Nếu nguồn dữ liệu tồn tại, phản hồi là mã trạng thái
200 OK
. Phần nội dung phản hồi chứa nội dung biểu diễn JSON của nguồn dữ liệu. - Lệnh 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"
Lấy dữ liệu tổng hợp
Ví dụ này minh hoạ cách truy vấn một nguồn dữ liệu cụ thể cho dữ liệu
tổng hợp, trong trường hợp này là estimated_steps
, nguồn dữ liệu dùng để hiển thị
số bước trong ứng dụng Google Fit. Hãy lưu ý rằng dấu thời gian trong nội dung yêu cầu JSON
tính bằng mili giây.
- Phương thức HTTP
- ĐĂNG
- URL yêu cầu
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
- Nội dung yêu cầu
{ "aggregateBy": [{ "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps" }], "bucketByTime": { "durationMillis": 86400000 }, "startTimeMillis": 1454284800000, "endTimeMillis": 1455062400000 }
- Phản hồi
Nếu(các) nguồn dữ liệu tồn tại, thì phản hồi là mã trạng thái
200 OK
. Phần nội dung phản hồi chứa nội dung biểu diễn JSON của nguồn dữ liệu.- Lệnh 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"
Cập nhật nguồn dữ liệu
Ví dụ này minh hoạ cách cập nhật tên và phiên bản thiết bị cho một nguồn dữ liệu.
- Phương thức HTTP
- UÚT
- URL yêu cầu
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
- Nội dung yêu cầu
{ "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" } }
- Phản hồi
Nếu nguồn dữ liệu được cập nhật thành công, phản hồi sẽ là mã trạng thái
200 OK
. Phần nội dung phản hồi chứa bản trình bày JSON của nguồn dữ liệu.- Lệnh 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"
Xóa nguồn dữ liệu
Ví dụ này minh họa cách xóa nguồn dữ liệu.
- Phương thức HTTP
- XÓA
- URL yêu cầu
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
- Nội dung yêu cầu
- Không có
- Phản hồi
- Nếu nguồn dữ liệu đã được xoá thành công thì nội dung phản hồi sẽ là một mã trạng thái
200 OK
. Phần nội dung phản hồi chứa bản trình bày JSON của nguồn dữ liệu đã bị xoá. - Lệnh 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"