【问题标题】:Youtube API returns account details of a different userYoutube API 返回不同用户的帐户详细信息
【发布时间】: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


    【解决方案1】:

    不确定问题一,但对于问题二,您会获得名为 true 的频道的信息,因为您正在请求它。 forUsername: true

    我希望一旦您更正此问题,如果用户名有多个频道,则响应可能包含多个频道。

    【讨论】:

    • 愚蠢的我。好吧,考虑到我有登录帐户的谷歌令牌这一事实,有谁知道我需要使用哪些正确的参数才能获取他的基本信息?
    【解决方案2】:

    只是对有关基本信息的问题的跟进。

    您不使用 Youtube API 来获取帐户的个人资料信息。相反,请尝试Retrieve Profile Information with G+:

    要检索用户的个人资料信息,请使用 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 网站登录也启用了Getting profile information:

    使用默认范围通过 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());
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 2017-01-16
      • 2016-10-27
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多