Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In dieser Kurzanleitung wird beschrieben, wie Sie eine einfache Seite einrichten, die Anfragen an die YouTube Data API sendet. In dieser Kurzanleitung wird beschrieben, wie Sie zwei API-Anfragen stellen:
Sie verwenden einen API-Schlüssel, der Ihre Anwendung identifiziert, um Informationen zum YouTube-Kanal GoogleDevelopers abzurufen.
Sie verwenden eine OAuth 2.0-Client-ID, um eine autorisierte Anfrage zu senden, mit der Informationen zu Ihrem eigenen YouTube-Kanal abgerufen werden.
Vorbereitung
Für diese Kurzanleitung benötigen Sie Folgendes:
Python 2.4 oder höher (für einen Webserver)
Zugriff auf das Internet und einen Webbrowser
Ein Google-Konto.
Schritt 1: Projekt und Anmeldedaten einrichten
Erstellen oder wählen Sie ein Projekt in der API Console aus. Führen Sie in der API Console die folgenden Aufgaben für Ihr Projekt aus:
Suchen Sie in der Bibliothek nach der YouTube Data API v3. Klicken Sie auf den Eintrag für diese API und prüfen Sie, ob die API für Ihr Projekt aktiviert ist.
API-Schlüssel erstellen
Sie verwenden den API-Schlüssel, um API-Anfragen zu stellen, für die keine Nutzerautorisierung erforderlich ist. So benötigen Sie beispielsweise keine Nutzerautorisierung, um Informationen zu einem öffentlichen YouTube-Kanal abzurufen.
OAuth 2.0-Client-ID erstellen
Legen Sie den Anwendungstyp auf Webanwendung fest. Für Anfragen, die eine Nutzerautorisierung erfordern, müssen Sie OAuth 2.0-Anmeldedaten verwenden. Sie benötigen beispielsweise eine Nutzerautorisierung, um Informationen zum YouTube-Kanal des aktuell authentifizierten Nutzers abzurufen.
Geben Sie im Feld Autorisierte JavaScript-Quellen die URL http://localhost:8000 ein. Sie können das Feld Autorisierte Weiterleitungs-URIs leer lassen.
Schritt 2: Beispiel einrichten und ausführen
Verwenden Sie das APIs Explorer-Widget in der Seitenleiste, um Beispielcode zum Abrufen von Informationen zum YouTube-Kanal „GoogleDevelopers“ zu erhalten. Bei dieser Anfrage wird ein API-Schlüssel verwendet, um Ihre Anwendung zu identifizieren. Sie erfordert keine Nutzerautorisierung oder spezielle Berechtigungen des Nutzers, der das Beispiel ausführt.
Öffnen Sie die Dokumentation für die Methode channels.list der API.
Auf dieser Seite finden Sie im Abschnitt „Häufige Anwendungsfälle“ eine Tabelle, in der verschiedene gängige Verwendungsarten der Methode beschrieben werden. Der erste Eintrag in der Tabelle bezieht sich auf die Auflistung von Ergebnissen nach Channel-ID.
Klicken Sie auf das Codesymbol für den ersten Eintrag, um den Vollbild-APIs Explorer zu öffnen und auszufüllen.
Auf der linken Seite des Vollbild-APIs Explorer wird Folgendes angezeigt:
Unter der Überschrift Request parameters (Anfrageparameter) finden Sie eine Liste der Parameter, die von der Methode unterstützt werden. Die Parameterwerte part und id sollten festgelegt werden. Der Parameterwert id, UC_x5XG1OV2P6uZZ5FSM9Ttw, ist die ID für den YouTube-Kanal GoogleDevelopers.
Unter den Parametern befindet sich der Abschnitt Anmeldedaten. Im Drop-down-Menü in diesem Bereich sollte der Wert API-Schlüssel angezeigt werden. Im APIs Explorer werden standardmäßig Demozugangsdaten verwendet, um den Einstieg zu erleichtern. Sie verwenden jedoch Ihren eigenen API-Schlüssel, um das Beispiel lokal auszuführen.
Rechts im Vollbild-API Explorer werden Tabs mit Codebeispielen in verschiedenen Sprachen angezeigt. Wählen Sie den Tab JavaScript aus.
Kopieren Sie das Codebeispiel und speichern Sie es in einer Datei mit dem Namen example.html.
Suchen Sie in dem heruntergeladenen Beispiel nach dem String YOUR_API_KEY und ersetzen Sie ihn durch den API-Schlüssel, den Sie in Schritt 1 dieser Kurzanleitung erstellt haben.
Geben Sie den folgenden Befehl in Ihr Arbeitsverzeichnis ein, um den Webserver zu starten:
Python 2.x
python -m SimpleHTTPServer 8000
Python 3.x
python -m http.server 8000
Öffnen Sie die Datei „example.html“ in Ihrem Browser. Öffnen Sie auch die Entwicklertools des Browsers, z. B. die „Entwicklertools“ im Chrome-Browser.
Klicken Sie auf der Seite auf die Schaltfläche load (Laden), um die Google APIs Client Library for JavaScript zu laden. Nachdem Sie auf die Schaltfläche geklickt haben, sollte in der Entwicklerkonsole ein Hinweis angezeigt werden, dass der GAPI-Client geladen wurde.
Klicken Sie auf die Schaltfläche Ausführen, um die API-Anfrage zu senden. Die API-Antwort sollte dann in der Entwicklerkonsole angezeigt werden.
Schritt 3: Autorisierte Anfrage ausführen
In diesem Schritt ändern Sie das Codebeispiel so, dass anstelle von Informationen zum YouTube-Kanal „GoogleDevelopers“ Informationen zu Ihrem YouTube-Kanal abgerufen werden. Für diese Anfrage ist eine Nutzerautorisierung erforderlich.
Kehren Sie zur Dokumentation für die Methode channels.list der API zurück.
Klicken Sie im Abschnitt „Häufige Anwendungsfälle“ auf das Codesymbol für den dritten Eintrag in der Tabelle. In diesem Anwendungsfall wird die Methode list für „mychannel“ aufgerufen.
Auf der linken Seite des Vollbild-APIs Explorer sehen Sie wieder eine Liste von Parametern, gefolgt vom Abschnitt Anmeldedaten. Es gibt jedoch zwei Änderungen im Vergleich zum Beispiel, in dem Sie Informationen zum GoogleDevelopers-Kanal abgerufen haben:
Im Parameterabschnitt sollte der Parameterwert mine auf true festgelegt werden, anstatt den Parameterwert id festzulegen. Dadurch wird der API-Server angewiesen, Informationen zum Kanal des aktuell authentifizierten Nutzers abzurufen.
Im Bereich Anmeldedaten sollte im Drop-down-Menü die Option für Google OAuth 2.0 ausgewählt sein.
Wenn Sie auf den Link Bereiche anzeigen klicken, sollte außerdem der Bereich https://www.googleapis.com/auth/youtube.readonly aktiviert sein.
Wählen Sie wie im vorherigen Beispiel den Tab JavaScript aus, kopieren Sie das Codebeispiel und speichern Sie es unter example.html.
Suchen Sie im Code nach dem String YOUR_CLIENT_ID und ersetzen Sie ihn durch die Client-ID, die Sie in Schritt 1 dieses Schnellstarts erstellt haben.
Geben Sie den folgenden Befehl in Ihr Arbeitsverzeichnis ein, um den Webserver zu starten:
Python 2.x
python -m SimpleHTTPServer 8000
Python 3.x
python -m http.server 8000
Rufen Sie die Datei http://localhost:8000/example.html in Ihrem Browser auf.
Öffnen Sie die Entwicklertools des Browsers, z. B. die „Entwicklertools“ im Chrome-Browser.
Klicken Sie auf der Seite auf die Schaltfläche Autorisieren und laden, um die Google APIs Client Library for JavaScript zu laden und den Autorisierungsvorgang zu starten.
Sie werden aufgefordert, der Anwendung die Berechtigung zum Lesen von Daten aus Ihrem YouTube-Konto zu erteilen.
Wenn Sie die Berechtigung erteilen, sollten in der Entwicklerkonsole Meldungen angezeigt werden, die darauf hinweisen, dass die Anmeldung erfolgreich war und der API-Client geladen wurde.
Klicken Sie auf die Schaltfläche Ausführen, um die API-Anfrage zu senden. Die API-Antwort sollte dann in der Entwicklerkonsole angezeigt werden.
[null,null,["Zuletzt aktualisiert: 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)"]]