Chúng tôi đang cập nhật Data API để phù hợp với cách YouTube tính số lượt xem video ngắn.
Tìm hiểu thêm
Làm việc với mã nhận dạng kênh
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Jeff Posnick, Trưởng nhóm quan hệ đối tác nhà phát triển của YouTube – Tháng 6 năm 2013
Trong hơn một năm qua, bạn có thể liên kết hồ sơ Google+ với kênh YouTube. Chúng tôi đã đăng trên blog về cách một số phản hồi của Data API phiên bản 2 đã thay đổi do liên kết hồ sơ đó. Gần đây, bạn có thể tạo kênh YouTube mới không liên kết với tên người dùng YouTube truyền thống mà chỉ được xác định bằng hồ sơ Google+. Phần lớn thông tin trong bài đăng trên blog đó vẫn áp dụng, nhưng vấn đề này đã làm mất hiệu lực một số giả định cơ bản về kênh YouTube (chẳng hạn như mỗi kênh sẽ luôn được liên kết với một tên người dùng YouTube duy nhất). Vì vậy, chúng tôi muốn chia sẻ thêm một số phương pháp hay nhất để viết mã phù hợp với mọi loại kênh.
Mã nhận dạng kênh trong Data API phiên bản 3
Tất cả các thao tác v3 hoạt động với kênh đều sử dụng mã nhận dạng kênh để xác định các kênh đó. Mã nhận dạng cho kênh của một người dùng YouTube cụ thể giống nhau trong cả API phiên bản 2 và phiên bản 3, giúp đơn giản hoá việc di chuyển giữa các phiên bản. Việc hoàn toàn dựa vào mã nhận dạng kênh có thể gây khó khăn cho những nhà phát triển trước đây đã quen với việc truyền tên người dùng trên YouTube đến các phương thức API. Tuy nhiên, phiên bản 3 được thiết kế để xử lý các kênh có và không có tên người dùng cũ giống nhau, tức là sử dụng mã nhận dạng kênh ở mọi nơi.
Nếu đang sử dụng phiên bản 3 và muốn truy xuất mã nhận dạng kênh tương ứng với người dùng hiện được uỷ quyền, bạn có thể gọi phương thức channels.list(part="id", mine=true)
. Điều này tương đương với việc yêu cầu hồ sơ kênh của người dùng default
trong phiên bản 2.
Nếu có lúc bạn cần chuyển đổi tên người dùng YouTube cũ tuỳ ý thành mã nhận dạng kênh bằng API phiên bản 3, bạn có thể thực hiện lệnh gọi channels.list(part="id", forUsername="username")
đến API.
Nếu bạn chỉ biết tên hiển thị và muốn tìm kênh tương ứng, thì phương thức search.list(part="snippet", type="channel", q="display name")
sẽ rất hữu ích. Bạn nên chuẩn bị để xử lý khả năng lệnh gọi trả về nhiều mục trong phản hồi, vì tên hiển thị không phải là duy nhất.
Mã nhận dạng kênh trong Data API phiên bản 2
Lưu ý: Chúng tôi đã ngừng sử dụng YouTube Data API (phiên bản 2) kể từ ngày 26 tháng 2 năm 2014 và đã ngừng cung cấp API này. Các ứng dụng vẫn sử dụng API phiên bản 2 phải di chuyển sang API phiên bản 3 ngay lập tức.
Điểm quan trọng nhất mà nhà phát triển cần lưu ý khi sử dụng Data API v2 cũ là không phải kênh YouTube nào cũng có tên người dùng riêng biệt. May mắn là mỗi kênh YouTube đều có một mã nhận dạng kênh riêng biệt được liên kết với kênh đó, được biểu thị bằng giá trị trong thẻ <yt:channelId>
. Đây là giá trị mà nhà phát triển nên sử dụng thay vì tên người dùng. Ví dụ: nếu bạn có cơ sở dữ liệu liên kết tên người dùng trên YouTube với thông tin về kênh đó, thì các mục cũ của bạn sẽ tiếp tục hoạt động. (Các kênh hiện có sẽ không mất tên người dùng.) Tuy nhiên, theo thời gian, bạn sẽ ngày càng phải làm việc với những kênh không thể xác định duy nhất bằng tên người dùng.
Có một số yếu tố giúp đơn giản hoá quá trình chuyển đổi từ tên người dùng sang mã nhận dạng kênh. Trước tiên, Data API v2 chấp nhận mã nhận dạng kênh trong URL yêu cầu bất cứ khi nào chấp nhận tên người dùng trên YouTube. Điều này có nghĩa là bạn có thể hoán đổi mã nhận dạng kênh vào mã hiện có một cách liền mạch. Ví dụ: vì UC_x5XG1OV2P6uZZ5FSM9Ttw
là mã nhận dạng kênh của kênh có tên người dùng cũ là GoogleDevelopers
, nên hai URL sau đây là các yêu cầu API tương đương:
https://gdata.youtube.com/feeds/api/users/GoogleDevelopers?v=2.1
https://gdata.youtube.com/feeds/api/users/UC_x5XG1OV2P6uZZ5FSM9Ttw?v=2.1
Một điều khác cần lưu ý là bất cứ khi nào tạo yêu cầu v2 đã xác thực, bạn không bao giờ cần thêm tên người dùng của kênh được uỷ quyền khi tạo URL yêu cầu. Bạn luôn có thể sử dụng giá trị default
thay cho tên người dùng (hoặc mã nhận dạng kênh). Ví dụ: nếu muốn truy xuất nguồn cấp dữ liệu video tải lên cho người dùng hiện được uỷ quyền, bạn có thể thực hiện việc này tại https://gdata.youtube.com/feeds/api/users/default/uploads?v=2.1
.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2024-11-23 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2024-11-23 UTC."],[[["\u003cp\u003eYouTube channels can now be identified solely by their Google+ profile, without a traditional YouTube username.\u003c/p\u003e\n"],["\u003cp\u003eThe YouTube Data API v3 exclusively uses channel IDs to identify channels, offering consistency for channels with or without legacy usernames.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can use the \u003ccode\u003echannels.list\u003c/code\u003e method in v3 to retrieve a channel ID based on the authorized user or a legacy username.\u003c/p\u003e\n"],["\u003cp\u003eIn v2, it's crucial to recognize that not every YouTube channel has a unique username, but all have a unique channel ID, which is the recommended identifier.\u003c/p\u003e\n"],["\u003cp\u003eThe deprecated Data API v2 can use channel IDs in place of usernames in request URLs, and \u003ccode\u003edefault\u003c/code\u003e can be used instead of usernames for authenticated v2 requests.\u003c/p\u003e\n"]]],["YouTube introduced channels solely identified by Google+ profiles, lacking traditional usernames. Data API v3 exclusively uses channel IDs to identify channels, simplifying the process for developers. To get the authorized user's channel ID, use `channels.list(part=\"id\", mine=true)`. To translate a username to a channel ID, use `channels.list(part=\"id\", forUsername=\"username\")`. For display names, use `search.list(part=\"snippet\", type=\"channel\", q=\"display name\")`. Data API v2, now deprecated, also utilizes channel IDs, allowing them to replace usernames in requests.\n"],null,["# Work with Channel IDs\n\n*Jeff Posnick, YouTube Developer Relations -- June 2013*\nFor more than a year, it's been possible to link Google+ profiles with YouTube channels, and we've [blogged](http://apiblog.youtube.com/2012/03/youtube-google-api-and-you.html) about how some of the Data API v2 responses have changed as a result of that profile link. More recently, it has become possible to create new YouTube channels that don't have a traditional YouTube username associated with them and, instead, are [solely identified by their Google+ profile](http://youtubecreator.blogspot.com/2013/04/using-google-page-identity-on-youtube.html). Much of the information from that blog post still applies, but this extra wrinkle does invalidate some fundamental assumptions about YouTube channels -- like that each one will always be associated with a unique YouTube username -- and we wanted to follow up with some additional best practices to write code that works with all flavors of channels.\n\nChannel IDs in the Data API v3\n------------------------------\n\nAll v3 operations that work with channels use [channel IDs](/youtube/v3/docs/channels#id) exclusively as a means of identifying those channels. The ID for a specific YouTube user's channel is identical in both v2 and v3 of the API, simplifying migrations between versions. This complete reliance on channel IDs might be perplexing for developers who were previously used to passing YouTube usernames to API methods, but v3 was designed to treat channels with and without legacy usernames identically, and that means using channel IDs everywhere.\n\nIf you are using v3 and want to retrieve the channel ID that corresponds to the currently authorized user, you can call the [channels.list(part=\"id\", mine=true)](https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.channels.list?part=id&mine=true) method. This is equivalent to asking for the channel profile of the `default` user in v2.\n\nIf you ever do find yourself with an arbitrary legacy YouTube username that you need to translate into a channel ID using v3 of the API, you can make a [channels.list(part=\"id\", forUsername=\"\u003cvar class=\"apiparam\" translate=\"no\"\u003eusername\u003c/var\u003e\")](https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.channels.list?part=id&forUsername=YouTubeDev) call to the API.\n\nIf you only know a display name and are looking to find the corresponding channel, the [search.list(part=\"snippet\", type=\"channel\", q=\"\u003cvar class=\"apiparam\" translate=\"no\"\u003edisplay name\u003c/var\u003e\")](https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&q=YouTube+for+Developers&type=channel) method will come in handy. You should be prepared to deal with the possibility of the call returning more than one item in the response, since display names are not unique.\n\nChannel IDs in the Data API v2\n------------------------------\n\n**Note:** The YouTube Data API (v2) has been deprecated since February 26, 2014, and the API has been [turned down](http://youtube-eng.blogspot.com/2015/04/bye-bye-youtube-data-api-v2.html). Applications still using the v2 API should migrate to the v3 API immediately.\n\nThe biggest takeaway for developers using the older [Data API v2](/youtube/2.0/developers_guide_protocol_audience) is that you must be aware that **not every YouTube channel has a unique username** . Fortunately, every YouTube channel is guaranteed to have a unique channel ID associated with it, represented by the value in the [`\u003cyt:channelId\u003e` tag](/youtube/2.0/reference#youtube_data_api_tag_yt:channelId), and that's the value that we recommend developers use instead of usernames. For instance, if you have a database that maps YouTube usernames to information about that channel, your older entries should continue to work. (Existing channels won't lose their usernames.) However, as time goes on, it will become more and more likely that you'll have to work with channels that can't be uniquely identified by a username.\n\nA couple of factors simplify the transition from usernames to channel IDs. First, the Data API v2 accepts channel IDs in request URLs wherever it accepts YouTube usernames, meaning that you can seamlessly swap a channel ID into your existing code. For example, since `UC_x5XG1OV2P6uZZ5FSM9Ttw` is the channel ID for the channel with the legacy username `GoogleDevelopers`, the following two URLs are equivalent API requests: \n\n https://gdata.youtube.com/feeds/api/users/GoogleDevelopers?v=2.1\n https://gdata.youtube.com/feeds/api/users/UC_x5XG1OV2P6uZZ5FSM9Ttw?v=2.1\n\nAnother thing to keep in mind is that whenever you're making [authenticated](/youtube/2.0/developers_guide_protocol_authentication) v2 requests, you never need to include the authorized channel's username when constructing request URLs. You can always use the value `default` in place of a username (or channel ID). So if you want to retrieve the video uploads feed for the currently authorized user, for instance, you can do so at `https://gdata.youtube.com/feeds/api/users/default/uploads?v=2.1`."]]