Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa guida rapida spiega come configurare una pagina semplice che invia richieste
all'API YouTube Data. Questa guida rapida spiega come effettuare due richieste API:
Utilizzerai una chiave API, che identifica la tua applicazione, per
recuperare informazioni sul canale YouTube GoogleDevelopers.
Utilizzerai un ID client OAuth 2.0 per inviare una richiesta autorizzata
che recupera informazioni sul tuo canale YouTube.
Prerequisiti
Per eseguire questa guida rapida, devi disporre di:
Python 2.4 o versioni successive (per fornire un server web)
Accesso a internet e a un browser web.
Un Account Google.
Passaggio 1: configura il progetto e le credenziali
Crea o seleziona un progetto nella console API. Completa le seguenti attività nella console API per il tuo progetto:
Nel riquadro della libreria,
cerca l'API YouTube Data v3. Fai clic sulla scheda dell'API e
assicurati che sia abilitata per il tuo progetto.
Crea una chiave API
Utilizzerai la chiave API per effettuare richieste API che non richiedono l'autorizzazione dell'utente. Ad esempio, non è necessaria l'autorizzazione
dell'utente per recuperare informazioni su un canale YouTube pubblico.
Crea un ID client OAuth 2.0
Imposta il tipo di applicazione su Applicazione web. Devi utilizzare le credenziali OAuth
2.0 per le richieste che richiedono l'autorizzazione dell'utente. Ad
esempio, devi disporre dell'autorizzazione dell'utente per recuperare informazioni sul
canale YouTube dell'utente attualmente autenticato.
Nel campo Origini JavaScript autorizzate, inserisci l'URL
http://localhost:8000. Puoi lasciare vuoto il campo URI di reindirizzamento autorizzati.
Passaggio 2: configura ed esegui l'esempio
Utilizza il widget Explorer API nel riquadro laterale per ottenere un codice di esempio per
recuperare informazioni sul canale YouTube GoogleDevelopers. Questa richiesta
utilizza una chiave API per identificare l'applicazione e non richiede l'autorizzazione dell'utente o autorizzazioni speciali da parte dell'utente che esegue l'esempio.
Apri la documentazione del metodo
channels.list dell'API.
In questa pagina, la sezione "Casi d'uso comuni" contiene una tabella che spiega
diversi modi comuni in cui viene utilizzato il metodo. Il primo elenco nella tabella
riguarda i risultati degli elenchi per ID canale.
Fai clic sul simbolo del codice per il primo elenco per aprire e compilare
Explorer API a schermo intero.
Il lato sinistro di Explorer API a schermo intero mostra quanto segue:
Sotto l'intestazione Parametri della richiesta, è presente un elenco di parametri
supportati dal metodo. I valori dei parametri part e id devono
essere impostati. Il valore del parametro id, UC_x5XG1OV2P6uZZ5FSM9Ttw, è l'ID del canale YouTube GoogleDevelopers.
Sotto i parametri, c'è una sezione denominata Credenziali. Il
menu a discesa in questa sezione dovrebbe mostrare il valore Chiave API. L'API Explorer utilizza le credenziali demo per impostazione predefinita per semplificare l'inizio. Tuttavia, utilizzerai la tua chiave API per eseguire l'esempio in locale.
Il lato destro di Explorer API a schermo intero mostra schede con esempi di codice
in diverse lingue. Seleziona la scheda JavaScript.
Copia il codice di esempio e salvalo in un file denominato
example.html.
Nell'esempio che hai scaricato, trova la stringa YOUR_API_KEY e
sostituiscila con la chiave API che hai creato nel passaggio 1 di questa guida rapida.
Avvia il server web utilizzando il seguente comando dalla tua directory di lavoro:
Python 2.x
python -m SimpleHTTPServer 8000
Python 3.x
python -m http.server 8000
Apri il file example.html nel browser. Apri anche gli strumenti per sviluppatori del browser, ad esempio "Strumenti per sviluppatori" nel browser Chrome.
Fai clic sul pulsante Carica nella pagina per caricare la libreria client delle API di Google per JavaScript. Dopo aver fatto clic sul pulsante, nella console
per sviluppatori dovrebbe essere visualizzata una nota che indica che il client GAPI è stato caricato.
Fai clic sul pulsante Esegui per inviare la richiesta API. La console
dello sviluppatore dovrebbe quindi mostrare la risposta dell'API.
Passaggio 3: esegui una richiesta autorizzata
In questo passaggio, modificherai l'esempio di codice in modo che, anziché recuperare
informazioni sul canale YouTube GoogleDevelopers, recuperi informazioni
sul tuo canale YouTube. Questa richiesta richiede l'autorizzazione dell'utente.
Torna alla documentazione del metodo
channels.list dell'API.
Nella sezione "Casi d'uso comuni", fai clic sul simbolo del codice per la terza
voce dell'elenco nella tabella. Questo caso d'uso consiste nel chiamare il metodo list per "il mio
canale".
Anche in questo caso, sul lato sinistro di APIs Explorer a schermo intero, vedrai un elenco di parametri seguito dalla sezione Credenziali. Tuttavia, ci sono due modifiche rispetto all'esempio in cui hai recuperato informazioni sul canale GoogleDevelopers:
Nella sezione dei parametri, anziché impostare il valore del parametro id, il valore del parametro mine deve essere impostato su true. In questo modo
il server API recupera le informazioni sul canale dell'utente
attualmente autenticato.
Nella sezione Credenziali, il menu a discesa dovrebbe selezionare l'opzione per Google OAuth 2.0.
Inoltre, se fai clic sul link Mostra ambiti, l'ambito
https://www.googleapis.com/auth/youtube.readonly deve essere
selezionato.
Come nell'esempio precedente, seleziona la scheda JavaScript,
copia l'esempio di codice e salvalo in example.html.
Nel codice, trova la stringa YOUR_CLIENT_ID e sostituiscila con l'ID client che hai creato nel passaggio 1 di questa guida rapida.
Avvia il server web utilizzando il seguente comando dalla tua directory di lavoro:
Python 2.x
python -m SimpleHTTPServer 8000
Python 3.x
python -m http.server 8000
Vai al file http://localhost:8000/example.html nel browser.
Apri gli strumenti per sviluppatori del browser, ad esempio "Strumenti per sviluppatori" nel
browser Chrome.
Fai clic sul pulsante Autorizza e carica nella pagina per caricare la libreria client delle API di Google per JavaScript e avviare il flusso di autorizzazione.
Ti verrà chiesto di concedere all'applicazione l'autorizzazione a leggere i dati
del tuo account YouTube.
Se concedi l'autorizzazione, la console per gli sviluppatori dovrebbe visualizzare
messaggi che indicano che l'accesso è andato a buon fine e che il client API
è stato caricato.
Fai clic sul pulsante Esegui per inviare la richiesta API. La console
dello sviluppatore dovrebbe quindi mostrare la risposta dell'API.
[null,null,["Ultimo aggiornamento 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)"]]