我們正在更新 Data API,以符合 YouTube 計算 Shorts 觀看次數的方式。
瞭解詳情
實作:部分回應
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
以下範例說明如何在 YouTube Data API (v3) 中擷取部分 API 回應。
注意:API 的入門指南會進一步說明部分要求和回應。
v3 API 允許 (且實際上需要) 擷取部分資源,以免應用程式轉移、剖析及儲存不必要的資料。這麼做也可以確保 API 更有效率地使用網路、CPU 和記憶體資源。
API 支援兩個要求參數:part
和 fields
,可讓您識別應納入 API 回應的資源屬性。part
參數也會指出應由插入或更新資源的 API 要求設定的屬性。
請注意,如果更新要求未為先前含有值的資源屬性指定值,且符合下列條件,系統會刪除現有值:
範例
舉例來說,假設您想要更新下列 video
資源。(請注意,下方顯示的所有屬性都可以透過 API 更新,與範例無關的資源屬性已省略不列)。
{
"snippet": {
"title": "Old video title",
"description": "Old video description",
"tags": ["keyword1","keyword2","keyword3"],
"categoryId: 22
},
"status": {
"privacyStatus": "private",
"publishAt": "2014-09-01T12:00:00.0Z",
"license": "youtube",
"embeddable": True,
"publicStatsViewable": True
}
}
您呼叫 videos.update
方法,並將 part
參數值設為 snippet
。API 要求的主體包含下列資源:
{
"snippet": {
"title": "New video title",
"tags": ["keyword1","keyword2","keyword3"],
"categoryId: 22
}
}
這項要求會更新影片標題、刪除說明,但不會變更標記或類別 ID。由於要求未指定 snippet.description
屬性的值,因此系統會刪除影片說明。
status
物件中的屬性完全不受影響,因為 part
參數值並未將 status
納入要求更新的部分。事實上,如果 API 要求的主體包含 status
物件,API 會傳回 400 (Bad Request)
HTTP 回應,因為要求主體中包含意外的部分。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-23 (世界標準時間)。
[null,null,["上次更新時間:2024-11-23 (世界標準時間)。"],[[["\u003cp\u003eThe YouTube Data API (v3) requires retrieving partial resources to optimize data transfer, parsing, and storage.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003epart\u003c/code\u003e and \u003ccode\u003efields\u003c/code\u003e parameters enable users to specify which resource properties to include in API responses.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003epart\u003c/code\u003e parameter also dictates which properties are set during resource insertion or update requests.\u003c/p\u003e\n"],["\u003cp\u003eOmitting a previously valued property in an update request, within the specified \u003ccode\u003epart\u003c/code\u003e, will delete that property's value if modifiable.\u003c/p\u003e\n"],["\u003cp\u003eIncluding unexpected parts in an update request's body, meaning a part not listed in the \u003ccode\u003epart\u003c/code\u003e parameter, will result in a \u003ccode\u003e400 (Bad Request)\u003c/code\u003e HTTP response.\u003c/p\u003e\n"]]],["The YouTube Data API (v3) uses `part` and `fields` parameters to enable partial resource retrieval, improving efficiency by avoiding unnecessary data transfer. The `part` parameter identifies properties for retrieval and modification. When updating, omitting a previously valued property in the specified `part` results in its deletion. For example, updating a video resource's `snippet` part without including `description` deletes the description. Updating a property that is not in the part of the request being modified is not possible.\n"],null,["# Implementation: Partial responses\n\nThe following examples show how to retrieve partial API responses in the YouTube Data API (v3).\n\n**Note:** The API's [getting started](/youtube/v3/getting-started#partial) guide provides more detail about partial requests and responses.\n\nThe v3 API allows, and actually requires, the retrieval of partial resources so that applications avoid transferring, parsing, and storing unneeded data. This approach also ensures that the API uses network, CPU, and memory resources more efficiently.\n\nThe API supports two request parameters, `part` and `fields`, that enable you to identify the resource properties that should be included in API responses. The `part` parameter also identifies the properties that should be set by API requests that insert or update resources.\n\nNote that if an update request does not specify a value for a resource property that previously had a value, the existing value will be deleted if the following conditions are true:\n\n- The property's value can be modified by the request. (For example, when updating a `video` resource, you can update the value of the `snippet.description` property, but you cannot update the value of the `snippet.thumbnails` object.\n\n- The request's `part` parameter value identifies the resource part that contains the property.\n\n#### Example\n\nFor example, suppose you want to update the `video` resource shown below. (Note that all of the properties shown below can be updated via the API, and resource properties not relevant to the example have been omitted.) \n\n```\n{\n \"snippet\": {\n \"title\": \"Old video title\",\n \"description\": \"Old video description\",\n \"tags\": [\"keyword1\",\"keyword2\",\"keyword3\"],\n \"categoryId: 22\n },\n \"status\": {\n \"privacyStatus\": \"private\",\n \"publishAt\": \"2014-09-01T12:00:00.0Z\",\n \"license\": \"youtube\",\n \"embeddable\": True,\n \"publicStatsViewable\": True\n }\n}\n```\n\nYou call the [videos.update](/youtube/v3/docs/videos/update) method and set the `part` parameter value to `snippet`. The body of the API request contains the following resource: \n\n```\n{\n \"snippet\": {\n \"title\": \"New video title\",\n \"tags\": [\"keyword1\",\"keyword2\",\"keyword3\"],\n \"categoryId: 22\n }\n}\n```\n\nThis request updates the video's title, deletes its description, and does not change its tags or category ID. The video's description is deleted because the request does not specify a value for the `snippet.description` property.\n\nThe properties in the `status` object are not affected at all because the `part` parameter value did not include `status` as one of the parts that the request would update. In fact, if the body of the API request included the `status` object, the API would return a `400 (Bad Request)` HTTP response due to an [unexpected part](/youtube/v3/docs/errors#youtube.api.RequestContextError-badRequest-unexpectedPart) being included in the request body."]]