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.
Hướng dẫn bắt đầu nhanh này giải thích cách thiết lập một trang đơn giản để đưa ra yêu cầu đến YouTube Data API. Hướng dẫn nhanh này thực sự giải thích cách thực hiện 2 yêu cầu API:
Bạn sẽ sử dụng một khoá API (khoá này xác định ứng dụng của bạn) để truy xuất thông tin về kênh YouTube GoogleDevelopers.
Bạn sẽ sử dụng mã ứng dụng khách OAuth 2.0 để gửi một yêu cầu được uỷ quyền nhằm truy xuất thông tin về kênh YouTube của riêng bạn.
Điều kiện tiên quyết
Để chạy hướng dẫn bắt đầu nhanh này, bạn cần có:
Python 2.4 trở lên (để cung cấp máy chủ web)
Có quyền truy cập vào Internet và trình duyệt web.
Tài khoản Google.
Bước 1: Thiết lập dự án và thông tin đăng nhập
Tạo hoặc chọn một dự án trong Bảng điều khiển API. Hoàn tất các việc sau trong API Console cho dự án của bạn:
Trong bảng điều khiển thư viện, hãy tìm YouTube Data API phiên bản 3. Nhấp vào trang thông tin của API đó và đảm bảo rằng API đã được bật cho dự án của bạn.
Tạo khoá API
Bạn sẽ dùng khoá API để thực hiện các yêu cầu API không yêu cầu người dùng uỷ quyền. Ví dụ: bạn không cần có sự cho phép của người dùng để truy xuất thông tin về một kênh YouTube công khai.
Tạo mã ứng dụng OAuth 2.0
Đặt loại ứng dụng thành Ứng dụng web. Bạn cần sử dụng thông tin xác thực OAuth 2.0 cho những yêu cầu cần có sự uỷ quyền của người dùng. Ví dụ: bạn cần có sự uỷ quyền của người dùng để truy xuất thông tin về kênh YouTube của người dùng hiện đã xác thực.
Trong trường Các nguồn gốc JavaScript được cho phép, hãy nhập URL http://localhost:8000. Bạn có thể để trống trường URI chuyển hướng được uỷ quyền.
Bước 2: Thiết lập và chạy mẫu
Sử dụng tiện ích Trình khám phá API trong bảng điều khiển bên để lấy mã mẫu nhằm truy xuất thông tin về kênh YouTube GoogleDevelopers. Yêu cầu này sử dụng khoá API để xác định ứng dụng của bạn và không yêu cầu người dùng uỷ quyền hoặc bất kỳ quyền đặc biệt nào từ người dùng chạy mẫu.
Trên trang đó, phần "Các trường hợp sử dụng phổ biến" có một bảng giải thích một số cách phổ biến mà phương thức này được sử dụng. Mục đầu tiên trong bảng là để liệt kê kết quả theo mã nhận dạng kênh.
Nhấp vào biểu tượng mã cho mục đầu tiên để mở và điền thông tin vào Trình khám phá API ở chế độ toàn màn hình.
Phía bên trái của Trình khám phá API toàn màn hình cho thấy những nội dung sau:
Bên dưới tiêu đề Tham số yêu cầu, có một danh sách các tham số mà phương thức này hỗ trợ. Bạn nên đặt giá trị tham số part và id. Giá trị tham số id, UC_x5XG1OV2P6uZZ5FSM9Ttw, là mã nhận dạng của kênh YouTube GoogleDevelopers.
Bên dưới các tham số, có một phần tên là Credentials (Thông tin đăng nhập). Trình đơn kéo xuống trong phần đó sẽ hiển thị giá trị Khoá API. Theo mặc định, Trình khám phá API sử dụng thông tin đăng nhập minh hoạ để giúp bạn dễ dàng bắt đầu. Nhưng bạn sẽ dùng khoá API của riêng mình để chạy mẫu trên máy.
Phía bên phải của Trình khám phá API ở chế độ toàn màn hình cho thấy các thẻ có mẫu mã bằng nhiều ngôn ngữ. Chọn thẻ JavaScript.
Sao chép mẫu mã rồi lưu vào một tệp có tên là example.html.
Trong mẫu mà bạn đã tải xuống, hãy tìm chuỗi YOUR_API_KEY rồi thay thế chuỗi đó bằng khoá API mà bạn đã tạo ở bước 1 của hướng dẫn nhanh này.
Khởi động máy chủ web bằng lệnh sau trong thư mục đang hoạt động:
Python 2.x
python -m SimpleHTTPServer 8000
Python 3.x
python -m http.server 8000
Mở tệp example.html trong trình duyệt. Ngoài ra, hãy mở công cụ dành cho nhà phát triển của trình duyệt, chẳng hạn như "Công cụ cho nhà phát triển" trong trình duyệt Chrome.
Nhấp vào nút load (tải) trên trang để tải Google APIs Client Library cho JavaScript. Sau khi bạn nhấp vào nút này, bảng điều khiển dành cho nhà phát triển sẽ hiển thị một ghi chú cho biết rằng ứng dụng GAPI đã được tải.
Nhấp vào nút thực thi để gửi yêu cầu API. Sau đó, bảng điều khiển của nhà phát triển sẽ hiển thị phản hồi của API.
Bước 3: Chạy một yêu cầu được uỷ quyền
Trong bước này, bạn sẽ sửa đổi mẫu mã để thay vì truy xuất thông tin về kênh YouTube GoogleDevelopers, mẫu mã sẽ truy xuất thông tin về kênh YouTube của bạn. Yêu cầu này bắt buộc phải có sự uỷ quyền của người dùng.
Quay lại tài liệu về phương thức channels.list của API.
Trong phần "Các trường hợp sử dụng phổ biến", hãy nhấp vào biểu tượng mã cho mục thứ ba trong bảng. Trường hợp sử dụng đó là gọi phương thức list cho "kênh của tôi".
Một lần nữa, ở bên trái của Trình khám phá API ở chế độ toàn màn hình, bạn sẽ thấy danh sách các tham số, theo sau là phần Thông tin đăng nhập. Tuy nhiên, có 2 thay đổi so với ví dụ mà bạn truy xuất thông tin về kênh GoogleDevelopers:
Trong phần tham số, thay vì đặt giá trị tham số id, bạn nên đặt giá trị tham số mine thành true. Thao tác này hướng dẫn máy chủ API truy xuất thông tin về kênh của người dùng hiện đã xác thực.
Trong phần Credentials (Thông tin đăng nhập), trình đơn kéo xuống phải chọn lựa chọn Google OAuth 2.0.
Ngoài ra, nếu bạn nhấp vào đường liên kết Hiện phạm vi, bạn nên đánh dấu vào phạm vi https://www.googleapis.com/auth/youtube.readonly.
Tương tự như ví dụ trước, hãy chọn thẻ JavaScript, sao chép mã mẫu rồi lưu vào example.html.
Trong mã, hãy tìm chuỗi YOUR_CLIENT_ID rồi thay thế bằng mã ứng dụng mà bạn đã tạo ở bước 1 của hướng dẫn nhanh này.
Khởi động máy chủ web bằng lệnh sau trong thư mục đang hoạt động:
Python 2.x
python -m SimpleHTTPServer 8000
Python 3.x
python -m http.server 8000
Truy cập vào tệp http://localhost:8000/example.html trong trình duyệt.
Mở công cụ dành cho nhà phát triển của trình duyệt, chẳng hạn như "Công cụ dành cho nhà phát triển" trong trình duyệt Chrome.
Nhấp vào nút uỷ quyền và tải trên trang để tải Thư viện ứng dụng Google cho JavaScript và bắt đầu quy trình uỷ quyền.
Bạn sẽ được nhắc cấp cho ứng dụng quyền đọc dữ liệu từ tài khoản YouTube của bạn.
Nếu bạn cấp quyền, bảng điều khiển dành cho nhà phát triển sẽ hiển thị thông báo cho biết bạn đã đăng nhập thành công và ứng dụng API đã tải.
Nhấp vào nút thực thi để gửi yêu cầu API. Sau đó, bảng điều khiển của nhà phát triển sẽ hiển thị phản hồi của API.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-21 UTC."],[[["\u003cp\u003eThis guide demonstrates making two types of requests to the YouTube Data API: one using an API key for public data and another using OAuth 2.0 for user-specific data.\u003c/p\u003e\n"],["\u003cp\u003eTo begin, you must set up a project in the API Console, enable the YouTube Data API v3, and create both an API key and an OAuth 2.0 client ID, which will each serve a different purpose.\u003c/p\u003e\n"],["\u003cp\u003eThe guide uses the \u003ccode\u003echannels.list\u003c/code\u003e method to first retrieve information about the GoogleDevelopers YouTube channel using the API key and second retrieve information about the authenticated user's own channel using OAuth 2.0 credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code provided in the guide uses JavaScript, and the instructions include details for starting a local web server to run and test the API requests in a browser environment.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves replacing placeholder values for the API key and OAuth 2.0 client ID in the downloaded sample code, which is necessary for the requests to succeed, and also prompts for user authorization.\u003c/p\u003e\n"]]],["This guide demonstrates making YouTube Data API requests. First, set up a project in the API Console, enabling the YouTube Data API and creating an API key and an OAuth 2.0 client ID. Then, use the APIs Explorer to generate code for retrieving channel information, replacing placeholders with your API key or client ID. Run a web server and use a browser to send requests, first using the API key, and then, modifying the code to require user authorization with OAuth 2.0 to retrieve information about the user's own channel.\n"],null,["# JavaScript Quickstart\n\nThis quickstart guide explains how to set up a simple page that makes requests\nto the YouTube Data API. This quickstart actually explains how to make two API\nrequests:\n\n1. You will use an API key, which identifies your application, to retrieve information about the GoogleDevelopers YouTube channel.\n2. You will use an OAuth 2.0 client ID to submit an *authorized* request that retrieves information about your own YouTube channel.\n\n| **Note:** More generally, you can follow the instructions for the first example for any use case that uses an API key or the instructions for the second example for any use case that requires authorization using OAuth 2.0. See the [use cases and code samples tool](/youtube/v3/code_samples/code_snippets) for more examples.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you'll need:\n\n- Python 2.4 or greater (to provide a web server)\n- Access to the internet and a web browser.\n- A Google account.\n\nStep 1: Set up your project and credentials\n-------------------------------------------\n\nCreate or select a project in the [API Console](https://console.cloud.google.com/). Complete the following tasks in the API Console for your project:\n\n1. In the [library panel](https://console.developers.google.com/apis/library),\n search for the YouTube Data API v3. Click into the listing for that API and\n make sure the API is enabled for your project.\n\n2. In the [credentials\n panel](https://console.developers.google.com/apis/credentials),\n create two credentials:\n\n 1. **Create an API key**\n You will use the API key to make API requests that do\n not require user authorization. For example, you do not need user\n authorization to retrieve information about a public YouTube channel.\n\n 2. **Create an OAuth 2.0 client ID**\n\n Set the application type to **Web application**. You need to use OAuth\n 2.0 credentials for requests that require user authorization. For\n example, you need user authorization to retrieve information about the\n currently authenticated user's YouTube channel.\n\n In the **Authorized JavaScript origins** field, enter the URL\n `http://localhost:8000`. You can leave the **Authorized redirect URIs**\n field blank.\n\nStep 2: Set up and run the sample\n---------------------------------\n\nUse the APIs Explorer widget in the side panel to obtain sample code for\nretrieving information about the GoogleDevelopers YouTube channel. This request\nuses an API key to identify your application, and it does not require user\nauthorization or any special permissions from the user running the sample.\n\n1. Open the documentation for the API's [channels.list](/youtube/v3/docs/channels/list) method.\n2. On that page, the \"Common use cases\" section contains a table that explains\n several common ways that the method is used. The first listing in the table\n is for listing results by channel ID.\n\n Click the code symbol for the first listing to open and populate the\n fullscreen APIs Explorer.\n\n3. The left side of the fullscreen APIs Explorer shows the following:\n\n 1. Below the **Request parameters** header, there is a list of parameters\n that the method supports. The `part` and `id` parameter values should\n be set. The `id` parameter value, `UC_x5XG1OV2P6uZZ5FSM9Ttw`, is the\n ID for the GoogleDevelopers YouTube channel.\n\n 2. Below the parameters, there is a section named **Credentials** . The\n pulldown menu in that section should display the value **API key**. The\n APIs Explorer uses demo credentials by default to make it easier to get\n started. But you'll use your own API key to run the sample locally.\n\n4. The right side of the fullscreen APIs Explorer shows tabs with code samples\n in different languages. Select the **JavaScript** tab.\n\n5. Copy the code sample and save it in a file named\n `example.html`.\n\n6. In the sample that you downloaded, find the `YOUR_API_KEY` string and\n replace that with the API key that you created in step 1 of this quickstart.\n\n7. Start the web server using the following command from your working\n directory:\n\n ### Python 2.x\n\n python -m SimpleHTTPServer 8000\n\n ### Python 3.x\n\n python -m http.server 8000\n\n8. Open the example.html file in your browser. Also open the browser's\n developer tools, such as the \"Developer Tools\" in the Chrome browser.\n\n 1. Click the **load** button on the page to load the Google APIs Client\n Library for JavaScript. After you click the button, the developer\n console should display a note indicating that the GAPI client loaded.\n\n 2. Click the **execute** button to send the API request. The developer's\n console should then display the API response.\n\nStep 3: Run an authorized request\n---------------------------------\n\nIn this step, you'll modify your code sample so that instead of retrieving\ninformation about the GoogleDevelopers YouTube channel, it retrieves information\nabout *your* YouTube channel. This request does require user authorization.\n\n1. Go back to the documentation for the API's\n [channels.list](/youtube/v3/docs/channels/list) method.\n\n2. In the \"Common use cases\" section, click the code symbol for the third\n listing in the table. That use case is to call the `list` method for \"my\n channel.\"\n\n3. Again, in the left side of the fullscreen APIs Explorer, you will see a\n list of parameters followed by the **Credentials** section. However, there\n are two changes from the example where you retrieved information about the\n GoogleDevelopers channel:\n\n 1. In the parameters section, instead of the `id` parameter value being\n set, the `mine` parameter value should be set to `true`. This instructs\n the API server to retrieve information about the currently authenticated\n user's channel.\n\n 2. In the **Credentials** section, the pulldown menu should select the\n option for **Google OAuth 2.0**.\n\n In addition, if you click the **Show scopes** link, the\n **https://www.googleapis.com/auth/youtube.readonly** scope should be\n checked.\n\n4. As with the previous example, select the **JavaScript** tab,\n copy the code sample, and save it to `example.html`.\n\n In the code, find the `YOUR_CLIENT_ID` string and replace it with the\n client ID that you created in step 1 of this quickstart.\n5. Start the web server using the following command from your working\n directory:\n\n ### Python 2.x\n\n python -m SimpleHTTPServer 8000\n\n ### Python 3.x\n\n python -m http.server 8000\n\n6. Go to `http://localhost:8000/example.html` file in your browser.\n Open the browser's developer tools, such as the \"Developer Tools\" in the\n Chrome browser.\n\n7. Click the **authorize and load** button on the page to load the Google\n APIs Client Library for JavaScript and initiate the authorization flow.\n You should be prompted to grant the application permission to read data\n from your YouTube account.\n\n If you grant the permission, the developer's console should display\n messages indicating that sign-in was successful and that the API client\n loaded.\n8. Click the **execute** button to send the API request. The developer's\n console should then display the API response.\n\nFurther reading\n---------------\n\n- [Google Developers Console help documentation](/console/help/new)\n- [Google APIs Client Library for JavaScript documentation](/api-client-library/javascript)\n- [YouTube Data API reference documentation](/youtube/v3/docs)"]]