0
votes

I am using google's API for node.js https://www.npmjs.com/package/googleapis

I am trying to get an array of all channels which belong to the person who logged into my website with his google account. I am using this scope for this matter: ''https://www.googleapis.com/auth/youtube.readonly'

Now here is part of my code:

 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");


        });
    }

Now, in this console.log:

console.log(response.items[0].snippet);

I am getting the same info, no matter what account I am using to log into my website:

{ 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: '' } }

if I do console.log(response) which is the entire response

I get:

{ kind: 'youtube#channelListResponse',
  etag: '"m2yskBQFythfE4irbTIeOgYYfBU/ch97FwhvtkdYcbQGBeya1XtFqyQ"',
  pageInfo: { totalResults: 1, resultsPerPage: 5 },
  items:
   [ { kind: 'youtube#channel',
       etag: '"m2yskBQFythfE4irbTIeOgYYfBU/bBTQeJyetWCB7vBdSCu-7VLgZug"',
       id: 'UCG9p-zLTq1mO1KAwzN2h0YQ',
       snippet: [Object] } ] }

So, two problems here:

1) How do I get an array of owned channels by the logged user, inside the array I need objects which will represent each channel and basic info like channel name, profile pic.

2) why am I getting the info of some random youtube channel called "True"

2

2 Answers

0
votes

Not sure about question one but for question two you get the information for the channel called true because you are asking for it. forUsername: true

I would hope that once you correct this the response may contain more than one channel if the username has more than one.

0
votes

Just a follow up to the question about basic info.

You dont use Youtube API to get an account's profile information. Instead, try Retrieve Profile Information with G+:

To retrieve profile information for a user, use the people.get API method. To get profile information for the currently authorized user, use the userId value of me.

JavaScript example:

// 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 Sign-in for Websites also enables Getting profile information:

After you have signed in a user with Google using the default scopes, you can access the user's Google ID, name, profile URL, and email address.

To retrieve profile information for a user, use the getBasicProfile() method. For example:

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());
}