2016-02-13 34 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