处理会话
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Fitness REST API,您可以创建和获取时段。会话代表一个时间间隔
用户进行健身活动的时段
会话由 Users.session
资源表示。
activity 存储在 ActivityType 中
整数。
插入会话
此示例演示了如何插入会话。
- HTTP 方法
- PUT
- 请求网址
https://www.googleapis.com/fitness/v1/users/me/sessions/someSessionId
- 请求正文
{
"id": "someSessionId",
"name": "My example workout",
"description": "A very intense workout",
"startTimeMillis": 1396710000000,
"endTimeMillis": 1396713600000,
"version": 1,
"lastModifiedToken": "exampleToken",
"application": {
"detailsUrl": "http://example.com",
"name": "Foo Example App",
"version": "1.0"
},
"activityType": 1
}
- 响应
响应为 200 OK
状态代码。响应正文包含
会话。
- Curl 命令
$ curl --header "Authorization: Bearer ya29.1.yourtokenvalue" -X PUT \
--header "Content-Type: application/json;encoding=utf-8" -d @createsession.json \
"https://www.googleapis.com/fitness/v1/users/me/sessions/someSessionId"
列出现有会话
此示例演示了如何列出 2014 年 4 月的现有会话。
- HTTP 方法
- 获取
- 请求网址
https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=2014-04-01T00:00:00.000Z&endTime=2014-04-30T23:59:59.999Z
- 请求正文
- 无。
- 响应
- 响应为
200 OK
状态代码。响应正文包含所有请求的 JSON 表示,
与查询参数中提供的开始时间和结束时间相匹配的现有会话。
- Curl 命令
$ curl --header "Authorization: Bearer ya29.1.yourtokenvalue" -X GET \
--header "Content-Type: application/json;encoding=utf-8" \
"https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=2014-04-01T00:00:00.000Z&endTime=2014-04-30T23:59:59.999Z"
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Fitness REST API allows you to create and retrieve fitness sessions, which represent periods of user activity.\u003c/p\u003e\n"],["\u003cp\u003eSessions are managed using the \u003ccode\u003eUsers.session\u003c/code\u003e resource and contain data like start/end times, activity type, and optional metadata.\u003c/p\u003e\n"],["\u003cp\u003eYou can insert new sessions using a PUT request to the API with session details in the request body.\u003c/p\u003e\n"],["\u003cp\u003eExisting sessions can be listed by sending a GET request with optional start and end time parameters to filter results.\u003c/p\u003e\n"],["\u003cp\u003eFor best practices on handling user data, refer to the Responsible use of Google Fit guidelines.\u003c/p\u003e\n"]]],[],null,["# Working with Sessions\n\nThe Fitness REST API lets you create and obtain sessions. Sessions represent a time interval\nduring which users perform a fitness activity.\n\nSessions are represented by the [`Users.session`](/fit/rest/v1/reference/users/sessions) resource.\n\nActivities are stored in an ['ActivityType'](/fit/rest/v1/reference/activity-types)\ninteger.\n| **Important:** For best practices when managing user data, see [Responsible use of Google Fit](/fit/overview#responsible_use_of_google_fit).\n\nInsert a session\n----------------\n\nThis example demonstrates how to insert a session.\n\nHTTP method\n: PUT\n\nRequest URL\n: `https://www.googleapis.com/fitness/v1/users/me/sessions/`**someSessionId**\n\nRequest body\n:\n\n {\n \"id\": \"someSessionId\",\n \"name\": \"My example workout\",\n \"description\": \"A very intense workout\",\n \"startTimeMillis\": 1396710000000,\n \"endTimeMillis\": 1396713600000,\n \"version\": 1,\n \"lastModifiedToken\": \"exampleToken\",\n \"application\": {\n \"detailsUrl\": \"http://example.com\",\n \"name\": \"Foo Example App\",\n \"version\": \"1.0\"\n },\n \"activityType\": 1\n }\n\nResponse\n\n: The response is a `200 OK` status code. The response body contains a JSON representation of the\n session.\n\nCurl command\n:\n\n ```\n $ curl --header \"Authorization: Bearer ya29.1.yourtokenvalue\" -X PUT \\\n --header \"Content-Type: application/json;encoding=utf-8\" -d @createsession.json \\\n \"https://www.googleapis.com/fitness/v1/users/me/sessions/someSessionId\"\n ```\n\nList existing sessions\n----------------------\n\nThis example demonstrates how to list existing sessions from April 2014.\n\nHTTP method\n: GET\n\nRequest URL\n: `https://www.googleapis.com/fitness/v1/users/me/sessions`**?startTime=2014-04-01T00:00:00.000Z&endTime=2014-04-30T23:59:59.999Z**\n\nRequest body\n: None.\n\nResponse\n: The response is a `200 OK` status code. The response body contains JSON representations of all\n existing sessions that match the start and end times provided in the query parameters.\n\nCurl command\n:\n\n ```\n $ curl --header \"Authorization: Bearer ya29.1.yourtokenvalue\" -X GET \\\n --header \"Content-Type: application/json;encoding=utf-8\" \\\n \"https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=2014-04-01T00:00:00.000Z&endTime=2014-04-30T23:59:59.999Z\"\n ```"]]