2014-01-05 58 views
1

我試圖做如何在YouTube上接收頻道視頻的JSON列表?

我想顯示的已上傳到頻道的視頻列表。

我在哪裏至今

我明白,這是對用戶的JSON格式「上傳」正確的網址:

http://gdata.youtube.com/feeds/api/users/%USERNAME%/uploads?alt=json&v=2

,其中的用戶名是通道的名稱,而比對應於用戶頻道的字母/數字字符串(例如,MyChannel而不是hIANSUBhkj_baisu128)。

但是,當在我的用戶名(BenPearlMagic)上測試此項時,我收到一個非常長的JSON列表,其中包含大量似乎不相關的數據。

我的代碼是這樣的:

$.getJSON('http://gdata.youtube.com/feeds/api/users/benpearlmagic/uploads?alt=json', function(data) { 
    console.log(data); 
    for(var i=0; i<data.data.items.length; i++) { 
     console.log(data.data.items[i].title); // title 
     console.log(data.data.items[i].description); // description 
    } 
}); 

...但我無法得到它的工作。

回答

6

請參閱data.feed.entry數組,以獲取視頻列表。

$.getJSON('http://gdata.youtube.com/feeds/api/users/benpearlmagic/uploads?alt=json', function(data) { 
    console.log(data); 
    for(var i in data.feed.entry) { 
     console.log("Title : "+data.feed.entry[i].title.$t); // title 
     console.log("Description : "+data.feed.entry[i].content.$t); // description 
    } 
}); 

data.feed.entry[i]鍵:ID,發佈,更新,分類,標題,內容,鏈接,作者,國電電力$意見,YT $高清,媒體$組,GD $評級,YT $統計

+0

完美 - 謝謝。 – Ben

+0

太好了! ...雖然此解決方案將於2015年4月20日棄用! https://developers.google.com/youtube/terms#deprecation –

4

版本2已經近期內取消,谷歌只給你支持,直到2015年4月的新版本V3使用此,before generate your api developer key and enable YouTube Data API v3

https://developers.google.com/youtube/v3/getting-started

var videosURL = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId={myPlaylistID}&key={myAPIKey}&fields=items&part=snippet&maxResults=6&callback=?"; 


$.getJSON(videosURL, function(data) { 
$.each(data.items, function(i,val) { 
console.log(val.snippet.title); 
console.log(val.snippet.resourceId.videoId); 
}); 
}); 

如何讓你的playlistid?

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={myChannelName}&key={myAPIKey} 

的最愛,喜歡或上傳

相關問題