Monday 1 May 2017

Youtube API returns account details of a different user

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"



via user1938653

No comments:

Post a Comment