【发布时间】:2017-09-29 08:35:22
【问题描述】:
我正在为 node.js 使用谷歌的 API https://www.npmjs.com/package/googleapis
我正在尝试获取属于此人的所有频道的数组 谁用他的谷歌帐户登录我的网站。 我正在使用这个范围来解决这个问题: ''https://www.googleapis.com/auth/youtube.readonly'
现在这是我的代码的一部分:
app.get("/oauthcallback", function(req, res) {
//google redirected us back in here with random token
var code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) { //let's check if the query code is valid.
if (err) { //invalid query code.
console.log(err);
res.send(err);
return;
}
//google now verified that the login was correct.
googleAccountVerified(tokens, res); //now decide what to do with it
});
});
function googleAccountVerified(tokens, res){ //successfully verified.
//user was verified by google, continue.
oauth2Client.setCredentials(tokens); //save tokens to an object
//now ask google for the user's details
//with the verified tokens you got.
youtube.channels.list({
forUsername: true,
part: "snippet",
auth: oauth2Client
}, function (err, response) {
if(err) {
res.send("Something went wrong, can't get your google info");
return;
}
console.log(response.items[0].snippet);
res.send("test");
});
}
现在,在这个 console.log 中:
console.log(response.items[0].snippet);
无论我使用什么帐户登录我的网站,我都会获得相同的信息:
{ title: 'True',
description: '',
publishedAt: '2005-10-14T10:09:11.000Z',
thumbnails:
{ default: { url: 'https://i.ytimg.com/i/G9p-zLTq1mO1KAwzN2h0YQ/1.jpg?v=51448e08' },
medium: { url: 'https://i.ytimg.com/i/G9p-zLTq1mO1KAwzN2h0YQ/mq1.jpg?v=51448e08' },
high: { url: 'https://i.ytimg.com/i/G9p-zLTq1mO1KAwzN2h0YQ/hq1.jpg?v=51448e08' } },
localized: { title: 'True', description: '' } }
如果我执行 console.log(response),这是整个响应
我明白了:
{ kind: 'youtube#channelListResponse',
etag: '"m2yskBQFythfE4irbTIeOgYYfBU/ch97FwhvtkdYcbQGBeya1XtFqyQ"',
pageInfo: { totalResults: 1, resultsPerPage: 5 },
items:
[ { kind: 'youtube#channel',
etag: '"m2yskBQFythfE4irbTIeOgYYfBU/bBTQeJyetWCB7vBdSCu-7VLgZug"',
id: 'UCG9p-zLTq1mO1KAwzN2h0YQ',
snippet: [Object] } ] }
所以,这里有两个问题:
1) 我如何获得登录用户拥有的频道数组, 在数组中,我需要代表每个频道和基本信息(如频道名称、个人资料图片)的对象。
2) 为什么我会得到一些名为“True”的随机 youtube 频道的信息
【问题讨论】:
标签: javascript node.js oauth youtube-api