การเริ่มต้นใช้งาน REST API
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บทแนะนำนี้จะแสดงวิธีเปิดใช้งานการเข้าถึง Fitness REST API และรับสิทธิ์เข้าถึง OAuth
โทเค็น และเรียกใช้เมธอด API โดยใช้คำขอ HTTP
ในการใช้ REST API ของฟิตเนส คุณต้องเข้าใจพื้นฐานของ
บริการเว็บที่มีประสิทธิภาพ
และการแสดง JSON
รับบัญชี Google
หากต้องการใช้ฟิตเนส REST API คุณต้องมีบัญชี Google
หากมีบัญชีอยู่แล้ว คุณก็พร้อมใช้งานได้ คุณอาจต้องการ
สร้างบัญชี Google แยกต่างหากสำหรับวัตถุประสงค์ในการทดสอบ
ขอรหัสไคลเอ็นต์ OAuth 2.0
ทำตามขั้นตอนต่อไปนี้เพื่อขอรหัสไคลเอ็นต์ OAuth 2.0 สำหรับ Fitness API
- ไปที่หน้า
Google API Console
- เลือกโปรเจ็กต์หรือสร้างโปรเจ็กต์ใหม่ ใช้โปรเจ็กต์เดียวกันสำหรับ Android
และเวอร์ชัน REST ของแอป
- คลิกต่อไปเพื่อเปิดใช้ Fitness API
- คลิกไปที่ข้อมูลเข้าสู่ระบบ
- คลิกข้อมูลเข้าสู่ระบบใหม่ แล้วเลือกรหัสไคลเอ็นต์ OAuth
- ในส่วนประเภทแอปพลิเคชัน ให้เลือกเว็บแอปพลิเคชัน
- ในส่วนต้นทาง JavaScript ที่ได้รับอนุญาต ให้ป้อน URL ฐานของเว็บไซต์
คำขอจะเริ่มต้น (เช่น
https://developers.google.com
เป็น URL ที่ OAuth Playground ใช้)
- ในส่วน URI การเปลี่ยนเส้นทางที่ได้รับอนุญาต ให้ป้อน URL ของเว็บไซต์ที่มีการตอบกลับ
ระบบจะจัดการ (เช่น
https://developers.google.com/oauthplayground
เป็น URL ที่ OAuth Playground ใช้)
คลิกสร้าง รหัสไคลเอ็นต์ OAuth 2.0 และข้อมูลลับใหม่จะปรากฏในรายการ
รหัสสำหรับโปรเจ็กต์ รหัสไคลเอ็นต์ OAuth 2.0 คือสตริงอักขระ
ประมาณนี้
780816631155-gbvyo1o7r2pn95qc4ei9d61io4uh48hl.apps.googleusercontent.com
ลองใช้ REST API ใน OAuth Playground
OAuth Playground เป็นวิธีที่ง่ายที่สุดในการทำความคุ้นเคยกับ
Fitness REST API ด้วยการส่งคำขอ HTTP และสังเกตการตอบกลับก่อน
เขียนโค้ดไคลเอ็นต์
วิธีให้สิทธิ์ Fitness REST API ใน OAuth Playground
- ไปที่ OAuth Playground
- ในส่วนขั้นตอนที่ 1 เลือกและ ให้สิทธิ์ API ขยาย Fitness v1 แล้วเลือก
ขอบเขตฟิตเนสที่จะใช้
- คลิกปุ่ม Authorize API แล้วเลือกโครงการคอนโซล Google API
ให้ใช้ และคลิกอนุญาตเมื่อมีข้อความแจ้ง คุณจะสามารถเข้าถึงและ
แก้ไขข้อมูลที่เชื่อมโยงกับบัญชีคอนโซล Google API ที่เลือก
- คลิกปุ่มรหัสการให้สิทธิ์ของ Exchange สำหรับโทเค็น OAuth
Playground จะรวมส่วนหัวนี้ไว้ใน
Authorization:
โดยอัตโนมัติ
ส่วนหัวของคำขอเมื่อส่งคำขอ HTTP โปรดทราบว่าโทเค็นเพื่อการเข้าถึง
จะหมดอายุหลังผ่านไป 60 นาที (3,600 วินาที)
ส่งคำขอ HTTP
ตัวอย่างต่อไปนี้แสดงวิธีส่งคำขอ HTTP เพื่อแสดงรายการ
แหล่งข้อมูลที่ใช้ได้ และสร้างแหล่งข้อมูลใหม่ สำหรับ REST ฟิตเนส
API มีรูปแบบ URI ดังนี้
https://www.googleapis.com/fitness/v1/resourcePath?parameters
วิธีแสดงแหล่งข้อมูลทั้งหมดที่มี
- ในเมธอด HTTP ให้เลือก GET
- ใน URI คำขอ ให้ป้อน
https://www.googleapis.com/fitness/v1/users/me/dataSources
- คลิกส่งคำขอ
คำขอและการตอบกลับจะปรากฏที่ด้านขวาของหน้า หาก
คำขอประสบความสำเร็จ การตอบกลับจะแสดงแหล่งข้อมูลจาก
ตัวอย่างในรูปแบบ JSON
วิธีสร้างแหล่งข้อมูล
- ในเมธอด HTTP ให้เลือก POST
- ใน URI คำขอ ให้ป้อน
https://www.googleapis.com/fitness/v1/users/me/dataSources
- คลิกป้อนเนื้อหาของคำขอ
ในหน้าต่างเนื้อความของคำขอ ให้คัดลอกและวาง 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"
}
}
คลิกปิดในหน้าต่างเนื้อความของคำขอ
คลิกส่งคำขอ
คำขอและการตอบกลับจะปรากฏที่ด้านขวาของหน้า คำขอ
รวมโทเค็นเพื่อการเข้าถึง OAuth ในส่วนหัว Authorization
:
Authorization: Bearer ya29.OAuthTokenValue
หากคำขอประสบความสำเร็จ บรรทัดแรกของคำตอบจะเป็นดังนี้
HTTP/1.1 200 OK
ใช้ cURL เพื่อเข้าถึง Fit REST API
คุณสามารถใช้เครื่องมือบรรทัดคำสั่ง cURL เพื่อเข้าถึง
Fit REST API คุณจะต้องใช้โทเค็นเพื่อการเข้าถึง OAuth เพื่อส่งคำขอ
โดยใช้ cURL (ดูวิธีการก่อนหน้านี้)
โปรดทราบว่าโทเค็นเพื่อการเข้าถึงจะหมดอายุหลังจากผ่านไป 1 ชั่วโมง ตัวอย่างต่อไปนี้แสดง
Bash Script อย่างง่าย เพื่อแสดงแหล่งข้อมูลทั้งหมด
#!/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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\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)"]]