【问题标题】:How to implement youtube.search.list with Node.js如何使用 Node.js 实现 youtube.search.list
【发布时间】:2016-05-24 14:47:05
【问题描述】:

我正在尝试让youtube.search.listNode.js 一起工作。看起来我可以使用YouTube API v.3.0 成功进行身份验证,但是当我运行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) 并获取空对象。

【问题讨论】:

  • 我没有这方面的经验,但我会检查 developers.google.com/youtube/v3/code_samples/… 我认为在获得结果之前应该有一个 execute(),祝你好运,如果你得到它的工作,请报告。
  • @diynevala,我在您引用的资源上花了很多钱,但没有积极的结果

标签: javascript node.js youtube-api


【解决方案1】:

稍微摆弄一下 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 告诉一个人使用那个节点回调模式,结果证明是有效的。

【讨论】:

  • 这个例子正是我在评论中建议的(通过文档),但懒得让它成为答案。 :)
  • 谢谢。当我运行代码时,出现以下异常:request.execute() is not a function
  • 试试console.log(request);看看请求是什么。
  • @diynevala,正如我之前所说。我得到空对象
  • @Sirko。作品!非常感谢!回调方法做到了
猜你喜欢
  • 2012-02-11
  • 2011-05-24
  • 2014-11-04
  • 2020-03-08
  • 2013-03-07
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多