[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eYou can add nutrition data to Google Fit by creating a data source and using the \u003ccode\u003ecom.google.nutrition\u003c/code\u003e data type.\u003c/p\u003e\n"],["\u003cp\u003eEach data point represents the nutritional value of a meal or snack, including details like food item, meal type, and nutrient values.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides instructions and code examples for adding nutrition data to Google Fit using both Android and REST APIs.\u003c/p\u003e\n"],["\u003cp\u003eThe REST API allows you to create data sources and add nutrition data using HTTP requests with specific request bodies and URLs.\u003c/p\u003e\n"],["\u003cp\u003eYou can find code snippets and examples for creating data sources, adding nutrition data, and interacting with the Google Fit API in this document.\u003c/p\u003e\n"]]],[],null,["# Add Nutrition Data\n\nYou can add nutrition data to Google Fit by [creating a data source](#creating_a_data_source) and using\nthe [`com.google.nutrition`](/android/reference/com/google/android/gms/fitness/data/DataType#TYPE_NUTRITION) data type. Each data point represents the value\nof all nutrients consumed in a meal or snack. This example shows you how to add\nnutrition data for someone who's eaten a banana.\n\nCreating a data source\n----------------------\n\n### Android\n\nUse `DataSource.Builder` to create a new data source. For example, `nutritionSource`. \n\n val nutritionSource = DataSource.Builder()\n .setDataType(DataType.TYPE_NUTRITION)\n // ...\n .build()\n\n### REST\n\nCall the REST API to create a new data source. For example, `NutritionSource`.\n\n**HTTP method** \n\n POST\n\n**Request URL** \n\n https://www.googleapis.com/fitness/v1/users/me/dataSources\n\n**Request body** \n\n {\n \"dataStreamName\": \"NutritionSource\",\n \"type\": \"raw\",\n \"application\": {\n \"detailsUrl\": \"http://example.com\",\n \"name\": \"My Example App\",\n \"version\": \"1\"\n },\n \"dataType\": {\n \"name\": \"com.google.nutrition\",\n }\n }\n\n**Response**\n\nIf your data source was created successfully, you'll get a [`200 OK`](https://httpstatuses.com/200) HTTP\nresponse status code. The response body contains a JSON representation of\nthe data source, including a `datasource.dataStreamId` property. Use this ID\nas the `dataSourceId` to add data.\n\n**CURL command** \n\n```\n$ curl --header \"Authorization: Bearer ya29.yourtokenvalue --request POST \\\n--header \"Content-Type: application/json;encoding=utf-8\" --data @nutrition-ds.json \\\nhttps://www.googleapis.com/fitness/v1/users/me/dataSources\n```\n\nAdding nutrition data\n---------------------\n\n### Android\n\nThis example shows you how to create a new data point, and add nutrition\ndata for a banana, using the `nutritionSource` data source. \n\n val nutrients = mapOf(\n Field.NUTRIENT_TOTAL_FAT to 0.4f,\n Field.NUTRIENT_SODIUM to 1f,\n Field.NUTRIENT_SATURATED_FAT to 0.1f,\n Field.NUTRIENT_PROTEIN to 1.3f,\n Field.NUTRIENT_TOTAL_CARBS to 27.0f,\n Field.NUTRIENT_CHOLESTEROL to 0.0f,\n Field.NUTRIENT_CALORIES to 105.0f,\n Field.NUTRIENT_SUGAR to 14.0f,\n Field.NUTRIENT_DIETARY_FIBER to 3.1f,\n Field.NUTRIENT_POTASSIUM to 422f\n )\n val banana = DataPoint.builder(nutritionSource)\n .setTimestamp(timestamp, TimeUnit.MILLISECONDS)\n .setField(Field.FIELD_FOOD_ITEM, \"banana\")\n .setField(Field.FIELD_MEAL_TYPE, Field.MEAL_TYPE_SNACK)\n .setField(Field.FIELD_NUTRIENTS, nutrients)\n .build()\n\n### REST\n\nThis example shows you how to add a set of nutrition data using the\n`NutritionSource` data source. The values for the nutrition data type are\nnutrients (a map), meal type (4 = 'snack'), and the actual food item\n(a string).\n\n**HTTP method** \n\n PATCH\n\n**Request URL**\n\n\u003cbr /\u003e\n\n```html\n https://www.googleapis.com/fitness/v1/users/me/dataSources/datasource.dataStreamId/datasets/1574159699023000000-1574159699023999000\n \n```\n\n\u003cbr /\u003e\n\n**Request body**\n\n\u003cbr /\u003e\n\n```restructuredtext\n {\n \"minStartTimeNs\": 1574159699023000000,\n \"maxEndTimeNs\": 1574159699023999000,\n \"dataSourceId\": \"datasource.dataStreamId\",\n \"point\": [\n {\n \"startTimeNanos\": 1574159699023000000,\n \"endTimeNanos\": 1574159699023999000,\n \"dataTypeName\": \"com.google.nutrition\",\n \"value\": [\n {\n \"mapVal\": [\n {\n \"key\": \"fat.total\",\n \"value\": {\n \"fpVal\": 0.4\n }\n },\n {\n \"key\": \"sodium\",\n \"value\": {\n \"fpVal\": 1.0\n }\n },\n {\n \"key\": \"fat.saturated\",\n \"value\": {\n \"fpVal\": 0.1\n }\n },\n {\n \"key\": \"protein\",\n \"value\": {\n \"fpVal\": 1.3\n }\n },\n {\n \"key\": \"carbs.total\",\n \"value\": {\n \"fpVal\": 27.0\n }\n },\n {\n \"key\": \"cholesterol\",\n \"value\": {\n \"fpVal\": 0.0\n }\n },\n {\n \"key\": \"calories\",\n \"value\": {\n \"fpVal\": 105.0\n }\n },\n {\n \"key\": \"sugar\",\n \"value\": {\n \"fpVal\": 14.0\n }\n },\n {\n \"key\": \"dietary_fiber\",\n \"value\": {\n \"fpVal\": 3.1\n }\n },\n {\n \"key\": \"potassium\",\n \"value\": {\n \"fpVal\": 422.0\n }\n }\n ]\n },\n {\n \"intVal\": 4\n },\n {\n \"strVal\": \"banana\"\n }\n ]\n }\n ]\n }\n```\n\n\u003cbr /\u003e\n\n**Response**\n\nIf your data point was created successfully, you'll get a [`200 OK`](https://httpstatuses.com/200) HTTP\nresponse status code. The response body contains a JSON representation of\nthe data set.\n\n**CURL command** \n\n```\n$ curl --header \"Authorization: Bearer ya29.yourtokenvalue --request PATCH \\\n--header \"Content-Type: application/json;encoding=utf-8\" --data @nutrition-data.json \\\nhttps://www.googleapis.com/fitness/v1/users/me/dataSources/datasource.dataStreamId/datasets/1574159699023000000-1574159699023999000\n```\n| **Note:** Use `--request PATCH` (not `--request POST`) in your CURL command when adding data."]]