Node JS - Youtube API是空白的

3

我使用 npm install googleapis 命令下载并安装了Google APIs,现在我试图在我的Node.js文件中使用以下代码访问API:

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

然而,当我尝试访问视频对象时,总是返回null。 显然,youtube对象已损坏,因为当我将其字符串化时,我得到了这个结果:

{"_options":{"auth":"*********"},"activities":{},"captions":{},"channelBanners":{},"channelSections":{},"channels":{},"commentThreads":{},"comments":{},"guideCategories":{},"i18nLanguages":{},"i18nRegions":{},"liveBroadcasts":{},"liveStreams":{},"playlistItems":{},"playlists":{},"search":{},"subscriptions":{},"thumbnails":{},"videoAbuseReportReasons":{},"videoCategories":{},"videos":{},"watermarks":{},"google":{"_options":{},"auth":{"_cachedCredential":null}}}

所以所有小的“子对象”都是空的。我该怎么解决这个问题?

更新:所有的API都是以这种方式为空的。我尝试了“urlshortener”,但问题仍然存在。 - SalmonKiller
3个回答

0
你有检查过依赖项是否在你的 package.json 文件中吗?如果没有,请尝试使用 npm install --save googleapis 命令将其直接添加到你的依赖列表中。

0

当你将youtube变量转换为字符串时,它显示为空对象,并没有什么可担心的,因为该对象的JSON表示仅包含原始类型的属性。 youtube.videos对象仅包含方法,这些方法被JSON.stringify省略了。

尝试这个:

var google = require('googleapis');
var youtube = google.youtube({version: 'v3', auth: API_KEY});
var queryOptions = {
    'part': 'id,snippet',
    'maxResults': 5,
    'id': 'dQw4w9WgXcQ,HL1UzIK-flA'
};
youtube.videos.list(queryOptions, function(err, data) {
    if(err) {
        console.error(err);
        return;
    }

    console.log(data);
});

-2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接