2016-02-13 60 views
1

我試圖使youtube.search.listNode.js一起使用。它看起來像我可以用YouTube API v.3.0成功驗證,但是當我運行youtube.search.list時,我得到空的結果集。如何使用Node.js實現youtube.search.list

有代碼中的愚蠢的錯誤或我做出不正確的請求YouTube API

下面是代碼:

var google = require('googleapis'); 
var youtubeV3 = google.youtube({ version: 'v3', auth: 'MY_API_KEY' }); 

var request = youtubeV3.search.list({ 
    part: 'snippet', 
    type: 'video', 
    q: 'Cat', 
    maxResults: 50, 
    order: 'date', 
    safeSearch: 'moderate', 
    videoEmbeddable: true 
}); 

// console.log('Here we start search'); 
for (var i in request.items) { 
    var item = request.items[i]; 
    console.log('Title: ', item.id.videoId, item.snippet.title); 
} 

爲了檢查我從API回去我跑console.log(request),並得到空對象。

+1

我沒有這方面的經驗,但我會檢查https://developers.google.com/youtube/v3/code_samples/javascript#search_by_keyword 我認爲應該有一個execute()才能得到結果,祝你好運並報告回來,如果你得到它的工作。 – diynevala

+0

@diynevala,我在你引用的資源上花了很多錢,沒有積極的結果 – IgorM

回答

2

Fiddeling與周圍的API一點,我想出了以下工作代碼:

var google = require('googleapis'), 
    youtubeV3 = google.youtube({ version: 'v3', auth: 'API_KEY' }); 

var request = youtubeV3.search.list({ 
    part: 'snippet', 
    type: 'video', 
    q: 'Cat', 
    maxResults: 50, 
    order: 'date', 
    safeSearch: 'moderate', 
    videoEmbeddable: true 
}, (err,response) => { 
    // your code here 
}); 

儘管文檔here告訴一個使用執行,the repository of google apis on Github講述了一個使用該節點回調格局,其中原來工作。

+1

這個例子正是我在我的評論中提到的(通過文檔),但卻懶得作出回答。 :) – diynevala

+0

謝謝。當我運行代碼時,我得到以下異常:request.execute()不是函數 – IgorM

+1

嘗試'console.log(request);'查看請求是什麼。 – diynevala