開始使用 REST API
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本教學課程說明如何啟用 Fitness REST API 存取權並取得 OAuth 存取權
權杖,並透過 HTTP 要求叫用 API 方法。
如要使用 Fitness REST API,請務必瞭解
符合 REST 樣式的網路服務
和 JSON 表示法。
申請 Google 帳戶
您必須擁有 Google 帳戶,才能使用 Fitness REST API。
如果您已經有帳戶,則已準備就緒。你也可能需要完成
建立測試用的個別 Google 帳戶。
要求 OAuth 2.0 用戶端 ID
請按照下列步驟申請 Fitness API 的 OAuth 2.0 用戶端 ID。
- 前往
Google API 控制台。
- 選取專案,或建立新專案。使用同一個專案存取 Android
和 REST 版本
- 點選「繼續」以啟用 Fitness API。
- 按一下「前往憑證」。
- 按一下「New credentials」,然後選取「OAuth Client ID」。
- 在「Application type」(應用程式類型) 下方,選取 [Web application] (網頁應用程式)。
- 在「授權的 JavaScript 來源」下方輸入網站的基準網址
要求的來源 (例如
https://developers.google.com
是 OAuth Playground 使用的網址)。
- 在「授權的重新導向 URI」下方,輸入回應的網站網址
將由 Google 處理 (例如
https://developers.google.com/oauthplayground
是 OAuth Playground 使用的網址)。
點選「建立」。清單中會顯示新的 OAuth 2.0 用戶端 ID 和密鑰
設定專案 IDOAuth 2.0 用戶端 ID 是由一串字元組成的字串
如下所示:
780816631155-gbvyo1o7r2pn95qc4ei9d61io4uh48hl.apps.googleusercontent.com
在 OAuth Playground 中試用 REST API
要瞭解
提交 HTTP 要求並觀察回應內容,
完全不必撰寫用戶端程式碼
如何在 OAuth Playground 中授權 Fitness REST API:
- 前往 OAuth Playground。
- 在「步驟 1 選取與」授權 API、展開健身 v1,然後選取
要使用的健身範圍
- 按一下「Authorize APIs」(授權 API) 按鈕,選取要授予的 Google API 控制台專案
,並在系統提示時按一下「允許」。你將可存取
修改與所選 Google API 控制台帳戶相關聯的資料。
- 請按一下「Exchange License code for token」按鈕。OAuth
Playground 會自動在
Authorization:
中加入此標頭
要求標頭。請注意,存取權杖時
的有效期限為 60 分鐘 (3600 秒)。
提交 HTTP 要求
以下範例說明如何傳送 HTTP 要求,以列出所有清單
以及建立新的資料來源適合健身 REST 的
API,則 URI 格式為:
https://www.googleapis.com/fitness/v1/resourcePath?parameters
如要列出所有可用資料來源:
- 在「HTTP Method」中,選取「GET」。
- 在「Request URI」中,輸入
https://www.googleapis.com/fitness/v1/users/me/dataSources
。
- 按一下「傳送要求」。
要求和回應會顯示在頁面右側。如果
要求成功後,回應會顯示上一個
使用 JSON 格式的範例
如何建立資料來源:
- 在「HTTP Method」中選取「POST」。
- 在「Request URI」中,輸入
https://www.googleapis.com/fitness/v1/users/me/dataSources
。
- 按一下「輸入要求主體」。
在「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」。
按一下「傳送要求」。
要求和回應會顯示在頁面右側。要求
OAuth 存取權杖包含在 Authorization
標頭中:
Authorization: Bearer ya29.OAuthTokenValue
如果要求成功,回應的第一行為:
HTTP/1.1 200 OK
使用 cURL 存取 Fit REST API
您可以使用 cURL 指令列工具存取
Fit REST API。您必須具備 OAuth 存取權杖才能提出要求
使用 cURL (請參閱上方的操作說明)。
請注意,存取權杖會在一小時後失效。以下範例顯示
簡單的 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,請參閱下列頁面:
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\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,["# Getting Started with the REST API\n\nThis 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--------------------\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------------------------------\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----------------------------------------\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\n### Submit 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-----------------------------------\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----------\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)"]]