Javascript Youtube API返回不同用户的帐户详细信息
我正在为node.js使用google的API 我正在尝试获取属于此人的所有频道的数组 他用谷歌账号登录了我的网站。 我将此范围用于此事项: ''' 下面是我的部分代码:Javascript Youtube API返回不同用户的帐户详细信息,javascript,node.js,oauth,youtube-api,Javascript,Node.js,Oauth,Youtube Api,我正在为node.js使用google的API 我正在尝试获取属于此人的所有频道的数组 他用谷歌账号登录了我的网站。 我将此范围用于此事项: ''' 下面是我的部分代码: app.get("/oauthcallback", function(req, res) { //google redirected us back in here with random token var code = req.query.code; oauth2Clie
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) 为什么我会得到一个名为“真”的youtube随机频道的信息呢?关于第一个问题不太清楚,但对于第二个问题,你会得到名为“真”的频道的信息,因为你是在要求它
forUsername:true
如果用户名有多个频道,我希望在您更正此问题后,响应可能包含多个频道。只是关于基本信息问题的后续问题 您不会使用Youtube API获取帐户的个人资料信息。相反,请尝试: 要检索用户的配置文件信息,请使用people.get API方法。要获取当前授权用户的配置文件信息,请使用我的userId值 JavaScript示例:
// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
// https://developers.google.com/+/quickstart/javascript
gapi.client.load('plus','v1', function(){
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
console.log('Retrieved profile for:' + resp.displayName);
});
});
谷歌网站登录还支持:
使用默认作用域与Google登录用户后,您可以访问该用户的Google ID、名称、配置文件URL和电子邮件地址
要检索用户的配置文件信息,请使用getBasicProfile()方法。例如:
if (auth2.isSignedIn.get()) {
var profile = auth2.currentUser.get().getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
我真傻。那么,考虑到我有谷歌的登录账户代币,有人知道我需要使用哪些正确的参数才能获得他的基本信息吗?