2015-05-06 14 views
6

Voglio ottenere tutti i video di un singolo canale che ho il suo ID. Il problema che sto ricevendo solo le informazioni sul canale. questo è il link che sto usando:Ottenere tutti i video di un canale utilizzando l'API di youtube

https://gdata.youtube.com/feeds/api/users/UCdCiB_pNQpR0M_KkDG4Dz5A?v=2&alt=json&q=goal&orderby=published&max-results=10

+0

questo link non funziona !! – Fakher

+0

sì hai ragione questo è il link che mostra le informazioni sul canale: https://gdata.youtube.com/feeds/api/users/UCdCiB_pNQpR0M_KkDG4Dz5A?v=2&alt=json ma quando aggiungo alcuni parametri in URL, viene scelto ' lavoro! –

risposta

34

Questo collegamento è per l'API V2 ora in pensione, quindi non restituirà tutti i dati. Invece, ti consigliamo di utilizzare V3 dell'API. La prima cosa che devi fare è registrarti per una chiave API - puoi farlo creando un progetto allo console.developers.google.com, impostando l'API dei dati di YouTube su "on" e creando una chiave di accesso pubblica.

Dato che hai già l'ID del canale utente, puoi passare direttamente a ottenere i video da esso; notare, tuttavia, che se mai non si conosce l'ID del canale, è possibile ottenere in questo modo:

https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={username}&key={YOUR_API_KEY} 

Con l'ID del canale, è possibile ottenere tutti i video dal canale con l'endpoint di ricerca, come questo:

https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={channel id here}&maxResults=25&key={YOUR_API_KEY} 

In questo caso, ordinamento per data è lo stesso del vecchio parametro V2 per ordinare da "pubblicato".

Esistono anche molti altri parametri che è possibile utilizzare per recuperare video durante la ricerca di un canale; vedi https://developers.google.com/youtube/v3/docs/search/list per maggiori dettagli.

+1

viene generato un messaggio di errore: "reason": "keyInvalid", qual è la mia chiave API? –

+0

Devi andare su console.developers.google.com - lì dovrai creare un progetto. Una volta creato il progetto, selezionalo e scegli la voce di menu "API e autenticazione" nella barra laterale sinistra. Questo mostrerà tutte le possibili API ... ti consigliamo di scegliere "YouTube Data API" e impostarlo su "on". Infine, scegli la sezione "Credenziali" nella barra laterale di sinistra, dove sarai in grado di creare una "chiave di accesso pubblica". – jlmcdonald

+0

funziona :) grazie mille :) puoi dirmi come posso ottenere qualsiasi ID di canale in modo da poterlo aggiungere all'URL? –

2

Ho pensato di condividere il mio risultato finale utilizzando JavaScript. Utilizza la chiave dell'API di YouTube di Google e UserName per ottenere l'ID del canale, quindi trasferisce i video e i display in un elenco a un determinato tag div.

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>YouTube Channel Listing</title> 
    <script type="text/javascript"> 
     function getJSONData(yourUrl) { 
      var Httpreq = new XMLHttpRequest(); 
      try { 
       Httpreq.open("GET", yourUrl, false); 
       Httpreq.send(null); 
      } catch (ex) { 
       alert(ex.message); 
      } 
      return Httpreq.responseText; 
     } 
     function showVideoList(username, writediv, maxnumbervideos, apikey) { 
      try { 
       document.getElementById(writediv).innerHTML = ""; 
       var keyinfo = JSON.parse(getJSONData("https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=" + username + "&key=" + apikey)); 
       var userid = keyinfo.items[0].id; 
       var channeltitle = keyinfo.items[0].snippet.title; 
       var channeldescription = keyinfo.items[0].snippet.description; 
       var channelthumbnail = keyinfo.items[0].snippet.thumbnails.default.url; // default, medium or high 
       //channel header 
       document.getElementById(writediv).innerHTML += "<div style='width:100%;min-height:90px;'>" 
        + "<a href='https://www.youtube.com/user/" + username + "' target='_blank'>" 
        + "<img src='" + channelthumbnail + "' style='border:none;float:left;margin-right:10px;' alt='" + channeltitle + "' title='" + channeltitle + "' /></a>" 
        + "<div style='width:100%;text-align:center;'><h1><a href='https://www.youtube.com/user/" + username + "' target='_blank'>" + channeltitle + "</a></h1>" + channeldescription + "</div>" 
        + "</div>"; 
       var videoinfo = JSON.parse(getJSONData("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" + userid + "&maxResults=" + maxnumbervideos + "&key=" + apikey)); 
       var videos = videoinfo.items; 
       var videocount = videoinfo.pageInfo.totalResults; 
       // video listing 
       for (var i = 0; i < videos.length; i++) { 
        var videoid = videos[i].id.videoId; 
        var videotitle = videos[i].snippet.title; 
        var videodescription = videos[i].snippet.description; 
        var videodate = videos[i].snippet.publishedAt; // date time published 
        var videothumbnail = videos[i].snippet.thumbnails.default.url; // default, medium or high 
        document.getElementById(writediv).innerHTML += "<hr /><div style='width:100%;min-height:90px;'>" 
         + "<a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>" 
         + "<img src='" + videothumbnail + "' style='border:none;float:left;margin-right:10px;' alt='" + videotitle + "' title='" + videotitle + "' /></a>" 
         + "<h3><a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>" + videotitle + "</a></h3>" + videodescription + "" 
         + "</div>"; 
       } 
      } catch (ex) { 
       alert(ex.message); 
      } 
     } 
    </script> 
</head> 
<body> 
    <div id="videos"></div> 
    <script type="text/javascript"> 
     showVideoList("USER_NAME", "videos", 25, "YOUR_API_KEY"); 
    </script> 
</body> 
</html> 

OLTRE - ho anche scritto una funzione per gestire se si utilizza un ID di canale invece di un account in base UserName.

qui è che il codice:

 function showVideoListChannel(channelid, writediv, maxnumbervideos, apikey) { 
     try { 
      document.getElementById(writediv).innerHTML = ""; 
      var vid = getJSONData("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" + channelid + "&maxResults=" + (maxnumbervideos + 1) + "&key=" + apikey); 
      var videoinfo = JSON.parse(vid); 
      var videos = videoinfo.items; 
      var videocount = videoinfo.pageInfo.totalResults; 
      var content = "<div style='height:600px;overflow-y:auto;'>"; 
      for (var i = 0; i < videos.length - 1; i++) { 
       var videoid = videos[i].id.videoId; 
       var videotitle = videos[i].snippet.title; 
       var videodescription = videos[i].snippet.description; 
       var videodate = videos[i].snippet.publishedAt; // date time published 
       var newdate = new Date(Date.parse((videodate + " (ISO 8601)").replace(/ *\(.*\)/, ""))); 
       var min = newdate.getMinutes(); 
       if (min < 10) { 
        min = "0" + min; 
       } 
       if (newdate.getHours() > 12) { 
        newdate = newdate.getMonth() + 1 + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " " + (newdate.getHours() - 12) + ":" + min + " PM"; 
       } else if (newdate.getHours() == 12) { 
        newdate = newdate.getMonth() + 1 + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " " + newdate.getHours() + ":" + min + " PM"; 
       } else { 
        newdate = newdate.getMonth() + 1 + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " " + newdate.getHours() + ":" + min + " AM"; 
       } 
       var videothumbnail = videos[i].snippet.thumbnails.default.url; // default, medium or high 
       content += "<hr /><div style='width:100%;min-height:90px;'>" 
        + "<a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>" 
        + "<img src='" + videothumbnail + "' style='border:none;float:left;margin-right:10px;' alt='" + videotitle + "' title='" + videotitle + "' /></a>" 
        + "<h3><a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>" + videotitle + "</a></h3>" + videodescription + "<br />" 
        + "<span style='color:#738AAD;font-size:Small;'>" + newdate + "</span>" 
        + "</div>"; 
      } 
      content += "</div>"; 
      document.getElementById(writediv).innerHTML = content; 
     } catch (ex) { 
      alert(ex.message); 
     } 
    } 
+0

Ciao, per favore quale nome utente stai usando in questo codice sorgente? @Dr. Aaron Dishno – Rodrigo

+0

Esempio, se il percorso di YouTube è: https://www.youtube.com/user/SBCountyParks allora SBCountyParks è il nome utente.Ho scritto la pagina qui: http://www.sbcounty.gov/Main/Pages/YouTube.aspx avviso quando si fa clic su Parchi regionali, mostra i video da quel collegamento. –

Problemi correlati