REST API 使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本教程介绍如何激活对 Fitness REST API 的访问权限,获取 OAuth 访问权限
令牌,并使用 HTTP 请求调用 API 方法。
要使用 Fitness REST API,您必须了解
RESTful Web 服务
和 JSON 表示法。
获取 Google 账号
如要使用 Fitness REST API,您需要拥有一个 Google 账号。
如果您已有账号,那么就大功告成了。您可能还需要
创建一个单独的 Google 账号进行测试。
请求 OAuth 2.0 客户端 ID
请按照以下步骤为 Fitness API 请求 OAuth 2.0 客户端 ID。
- 前往
Google API 控制台。
- 选择一个项目,或创建一个新项目。为 Android 使用同一项目
和 REST 版本的应用
- 点击继续以启用 Fitness API。
- 点击转到凭据。
- 点击新建凭据,然后选择 OAuth 客户端 ID。
- 在应用类型下,选择网页应用。
- 在已获授权的 JavaScript 来源下,输入该网站的基准网址
发起请求的来源(例如
https://developers.google.com
)
是 OAuth 园地使用的网址)。
- 在已获授权的重定向 URI 下,输入响应所在网站的网址
系统将会处理(例如
https://developers.google.com/oauthplayground
)
是 OAuth 园地使用的网址)。
点击创建。您的新 OAuth 2.0 客户端 ID 和密钥会显示在列表中
多个项目 IDOAuth 2.0 客户端 ID 是一串字符
如下所示:
780816631155-gbvyo1o7r2pn95qc4ei9d61io4uh48hl.apps.googleusercontent.com
在 OAuth 园地中试用 REST API
要熟悉 OAuth 园地,最简单的方法就是使用
Fitness REST API,提交 HTTP 请求并观察响应,
您可以编写任何客户端代码。
如需在 OAuth Playground 中向 Fitness REST API 授权,请执行以下操作:
- 前往 OAuth Playground。
- 在第 1 步:选择和授权 API,展开 Fitness v1 并选择
要使用的健身范围。
- 点击授权 API 按钮,选择 Google API 控制台项目以
并在出现提示时点击允许。您将能够访问
修改与所选 Google API 控制台账号关联的数据。
- 点击 Exchange Authorization code for token 按钮。OAuth
Playground 会自动在
Authorization:
中添加此头文件
请求标头请注意,访问令牌
将在 60 分钟(3600 秒)后过期。
提交 HTTP 请求
以下示例演示了如何通过发送 HTTP 请求来列出所有
可用的数据源以及创建新的数据源。适用于 Fitness REST
则 URI 格式为:
https://www.googleapis.com/fitness/v1/resourcePath?parameters
列出所有可用数据源:
- 在 HTTP Method 中,选择 GET。
- 在请求 URI 中,输入
https://www.googleapis.com/fitness/v1/users/me/dataSources
- 点击 Send the request。
请求和响应显示在页面右侧。如果
请求成功后,响应会显示上一个
JSON 格式的示例。
如需创建数据源,请执行以下操作:
- 在 HTTP Method 中,选择 POST。
- 在请求 URI 中,输入
https://www.googleapis.com/fitness/v1/users/me/dataSources
- 点击 Enter request body(输入请求正文)。
在 Request Body 窗口中,复制并粘贴以下 JSON:
{
"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"
}
}
在 Request Body 窗口中,点击 Close。
点击 Send the request。
请求和响应显示在页面右侧。请求
在 Authorization
标头中包含 OAuth 访问令牌:
Authorization: Bearer ya29.OAuthTokenValue
如果请求成功,则响应的第一行将是:
HTTP/1.1 200 OK
使用 c网址 访问 Fit REST API
您可以使用 cURL 命令行工具访问
Fit REST API。您需要 OAuth 访问令牌才能发出请求
使用 c网址(请参阅上述说明)。
请注意,访问令牌会在一小时后过期。以下示例展示了
简单的 bash 脚本列出所有数据源。
#!/bin/bash
ACCESS_TOKEN=""
curl \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/fitness/v1/users/me/dataSources
后续步骤
如需详细了解 REST API,请参阅以下页面:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eThis tutorial provides a comprehensive guide on activating and utilizing the Fitness REST API for accessing and managing fitness data.\u003c/p\u003e\n"],["\u003cp\u003eIt outlines the process of obtaining OAuth access tokens for authentication and authorization.\u003c/p\u003e\n"],["\u003cp\u003eYou will learn how to make HTTP requests to interact with the API, including listing data sources and creating new ones.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial also provides practical examples using the OAuth Playground and cURL for testing and implementation.\u003c/p\u003e\n"],["\u003cp\u003eIt includes links to further resources for in-depth understanding of data types, sessions, and workout recording.\u003c/p\u003e\n"]]],[],null,["This tutorial shows you how to activate access to the Fitness REST API, obtain OAuth access\ntokens, and invoke the API methods using HTTP requests.\n\nTo use the Fitness REST API, it is important that you understand the basics of\n[RESTful web services](http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services)\nand [JSON representations](http://en.wikipedia.org/wiki/JSON).\n\nGet a Google Account\n\nTo use the Fitness REST API, you need a [Google Account](https://www.google.com/accounts/NewAccount).\nIf you already have an account, then you're all set. You may also want to\ncreate a separate Google Account for testing purposes.\n\nRequest an OAuth 2.0 client ID\n\nFollow these steps to request an OAuth 2.0 client ID for the Fitness API.\n\n1. Go to the [Google API Console](https://console.cloud.google.com/flows/enableapi?apiid=fitness).\n2. Select a project, or create a new one. Use the same project for the Android and REST versions of your app.\n3. Click **Continue** to enable the Fitness API.\n4. Click **Go to credentials**.\n5. Click **New credentials** , then select **OAuth Client ID**.\n6. Under **Application type** , select **Web application**.\n7. Under **Authorized JavaScript origins** , enter the base URL of the site from which requests will originate (for example `https://developers.google.com` is the URL used by the OAuth Playground).\n8. Under **Authorized redirect URI** , enter the URL of the site where responses will be handled (for example `https://developers.google.com/oauthplayground` is the URL used by the OAuth Playground).\n9. Click **Create**. Your new OAuth 2.0 Client ID and secret appear in the list\n of IDs for your project. An OAuth 2.0 Client ID is a string of characters,\n something like this:\n\n `780816631155-gbvyo1o7r2pn95qc4ei9d61io4uh48hl.apps.googleusercontent.com`\n\nTry the REST API in the OAuth Playground\n\nThe OAuth Playground is the easiest way to familiarize yourself with the\nFitness REST API by submitting HTTP requests and observing the responses before\nyou write any client code.\n\nTo authorize the Fitness REST API in the OAuth Playground:\n\n1. Go to the [OAuth Playground](https://developers.google.com/oauthplayground/).\n2. Under **Step 1 Select \\& authorize APIs** , expand **Fitness v1** and select the Fitness scopes to use.\n3. Click the **Authorize APIs** button, select the Google API Console project to use, and click **Allow** when prompted. You will be able to access and modify data associated with the selected Google API Console account.\n4. Click the **Exchange authorization code for tokens** button. The OAuth Playground automatically includes this header in the `Authorization:` request header when you submit HTTP requests. Note that the access token will expire after 60 minutes (3600 seconds).\n\nSubmit HTTP requests\n\nThe following examples demonstrate how to send HTTP requests to list all\navailable data sources, and to create a new data source. For the Fitness REST\nAPI, the URI format is:\n\n`https://www.googleapis.com/fitness/v1/`\u003cvar class=\"apiparam\" translate=\"no\"\u003eresourcePath\u003c/var\u003e`?`\u003cvar class=\"apiparam\" translate=\"no\"\u003eparameters\u003c/var\u003e\n\n**To list all available data sources:**\n\n1. In **HTTP Method** , select **GET**.\n2. In **Request URI** , enter `https://www.googleapis.com/fitness/v1/users/me/dataSources`\n3. Click **Send the request**.\n\nThe request and the response appear on the right side of the page. If the\nrequest is successful, the response shows the data source from the previous\nexample in JSON format.\n\n**To create a data source:**\n\n1. In **HTTP Method** , select **POST**.\n2. In **Request URI** , enter `https://www.googleapis.com/fitness/v1/users/me/dataSources`\n3. Click **Enter request body**.\n4. In the **Request Body** window, copy and paste the following JSON:\n\n {\n \"dataStreamName\": \"MyDataSource\",\n \"type\": \"derived\",\n \"application\": {\n \"detailsUrl\": \"http://example.com\",\n \"name\": \"Foo Example App\",\n \"version\": \"1\"\n },\n \"dataType\": {\n \"field\": [\n {\n \"name\": \"steps\",\n \"format\": \"integer\"\n }\n ],\n \"name\": \"com.google.step_count.delta\"\n },\n \"device\": {\n \"manufacturer\": \"Example Manufacturer\",\n \"model\": \"ExampleTablet\",\n \"type\": \"tablet\",\n \"uid\": \"1000001\",\n \"version\": \"1\"\n }\n }\n\n5. In the **Request Body** window, click **Close**.\n\n6. Click **Send the request**.\n\nThe request and the response appear on the right side of the page. The request\nincludes the OAuth access token in the `Authorization` header:\n\n`Authorization: Bearer ya29.`\u003cvar class=\"apiparam\" translate=\"no\"\u003eOAuthTokenValue\u003c/var\u003e\n\nIf the request is successful, the first line of the response is:\n\n`HTTP/1.1 200 OK`\n\nUse cURL to access the Fit REST API\n\nYou can use the [cURL](https://curl.haxx.se/) command line tool to access the\nFit REST API. You will need an OAuth access token to make requests\nusing cURL (see the preceding [instructions](#try_the_rest_api_in_the_oauth_playground)).\nNote that access tokens expire after an hour. The following example shows a\nsimple bash script to list all data sources. \n\n```bash\n#!/bin/bash\nACCESS_TOKEN=\"\"\ncurl \\\n -H \"Content-Type: application/json\" \\\n -H \"Authorization: Bearer $ACCESS_TOKEN\" \\\n https://www.googleapis.com/fitness/v1/users/me/dataSources\n```\n\nNext steps\n\nTo learn more about the REST API, see these pages:\n\n- [Fitness Data Types](/fit/rest/v1/data-types)\n- [Sessions](/fit/rest/v1/sessions)\n- [How to Record a Workout](/fit/rest/v1/workout)"]]