เรากำลังอัปเดต Data API ให้สอดคล้องกับวิธีที่ YouTube นับยอดดูสำหรับ Shorts
ดูข้อมูลเพิ่มเติม
การใช้งาน: คำตอบบางส่วน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ตัวอย่างต่อไปนี้แสดงวิธีเรียกข้อมูลการตอบกลับบางส่วนของ API ใน YouTube Data API (v3)
หมายเหตุ: คู่มือเริ่มต้นใช้งานของ API มีรายละเอียดเพิ่มเติมเกี่ยวกับคำขอและการตอบกลับบางส่วน
API เวอร์ชัน 3 อนุญาตและกำหนดให้ดึงข้อมูลบางส่วนเพื่อให้แอปพลิเคชันหลีกเลี่ยงการโอน การแยกวิเคราะห์ และการจัดเก็บข้อมูลที่ไม่จำเป็นต้องใช้ แนวทางนี้ยังช่วยให้ API ใช้ทรัพยากรเครือข่าย, CPU และหน่วยความจําได้อย่างมีประสิทธิภาพมากขึ้นด้วย
API รองรับพารามิเตอร์คําขอ 2 รายการ ได้แก่ part
และ fields
ซึ่งช่วยให้คุณระบุพร็อพเพอร์ตี้ทรัพยากรที่ควรรวมอยู่ในคําตอบของ API ได้ พารามิเตอร์ part
ยังระบุพร็อพเพอร์ตี้ที่ควรตั้งค่าตามคําขอ API ที่แทรกหรืออัปเดตทรัพยากรด้วย
โปรดทราบว่าหากคำขออัปเดตไม่ได้ระบุค่าสำหรับพร็อพเพอร์ตี้ทรัพยากรที่มีค่าก่อนหน้านี้ ระบบจะลบค่าที่มีอยู่หากเงื่อนไขต่อไปนี้เป็นจริง
-
คำขอสามารถแก้ไขค่าของพร็อพเพอร์ตี้ได้ (เช่น เมื่ออัปเดตทรัพยากร video
คุณจะอัปเดตค่าของพร็อพเพอร์ตี้ snippet.description
ได้ แต่จะอัปเดตค่าของออบเจ็กต์ snippet.thumbnails
ไม่ได้
-
ค่าพารามิเตอร์ part
ของคําขอจะระบุส่วนของทรัพยากรที่มีพร็อพเพอร์ตี้
ตัวอย่าง
ตัวอย่างเช่น สมมติว่าคุณต้องการอัปเดตแหล่งข้อมูล 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
}
}
คำขอนี้จะอัปเดตชื่อวิดีโอ ลบคำอธิบาย และจะไม่เปลี่ยนแท็กหรือรหัสหมวดหมู่ของวิดีโอ ระบบจะลบคำอธิบายของวิดีโอเนื่องจากคำขอไม่ได้ระบุค่าสำหรับพร็อพเพอร์ตี้ snippet.description
พร็อพเพอร์ตี้ในออบเจ็กต์ status
จะไม่ได้รับผลกระทบเลย เนื่องจากค่าพารามิเตอร์ part
ไม่ได้รวม status
ไว้เป็นหนึ่งในส่วนที่คำขอจะอัปเดต อันที่จริง หากเนื้อหาของคำขอ API มีออบเจ็กต์ status
อยู่ด้วย API จะแสดงผลการตอบกลับ HTTP 400 (Bad Request)
เนื่องจากมีส่วนที่ไม่คาดคิดรวมอยู่ในเนื้อหาคำขอ
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-11-23 UTC
[null,null,["อัปเดตล่าสุด 2024-11-23 UTC"],[[["\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."]]