Google OAuth2 登录 - 获取 YouTube 昵称和真实电子邮件地址

2021-12-31 00:00:00 oauth youtube youtube-api google-oauth php

有什么办法可以将用户的 youtube 昵称与他的真实 (*@gmail.com) 电子邮件地址一起获取?

Is there any way to get the users youtube nickname together with his REAL (*@gmail.com) email address?

每当我要求 google 以youtube.readonly"范围对用户进行身份验证时,电子邮件地址都会更改为*@pages.plusgoogle.com".但是当我离开 YouTube 范围时,我不会得到 YouTube 昵称...

Whenever I ask google to authenticate the user with "youtube.readonly" scope, the email address changes to "*@pages.plusgoogle.com". But when I leave out the YouTube scope I do not get the YouTube nickname...

由 Google API PHP 客户端执行的请求 (https://github.com/google/google-api-php-client)

1.正确的电子邮件地址/错误的姓名:

范围:

https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile

用户信息:

email: "********@gmail.com"         //<- I need this
family_name: <My last name>
given_name: <My first name>
name: <My full name>
verified_email: true
[...]

2.错误的电子邮件/正确的姓名:

范围:

https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/youtube.readonly

用户信息:

email: "<My YT nickname>-<Random(?) ID>@pages.plusgoogle.com"
family_name: "."
given_name: <My YT nickname>
name: <My YT nickname>              //<- and I need that
verified_email: true
[...]

那么:如何在不更改范围的情况下从请求 #1 和请求 #2 的名称中获取电子邮件(需要重新进行身份验证)?

So: How to get the email from request #1 and the name of request #2 without changing the scopes (what requires re-authentication)?

$oauth2 = new Google_Service_Oauth2($google);
if (strlen($code) > 10) {
    try {
        $accessToken = $google->authenticate($code);
    } catch (Google_Auth_Exception $e) {
        return new false;
    }

    if (!$accessToken) {
        return false;
    }

    $userinfo = $oauth2->userinfo->get();
    var_dump($userinfo);die;
}

转储 (使用 YT 范围)

object(Google_Service_Oauth2_Userinfo)[325]
  public 'email' => string '***@pages.plusgoogle.com' (length=36) //wrong email..
  public 'family_name' => string '.' (length=1)
  public 'gender' => null
  public 'given_name' => string '***' (length=10)
  public 'hd' => null
  public 'id' => string '1013***' (length=21)
  public 'link' => string 'https://plus.google.com/1013***' (length=45)
  public 'locale' => null
  public 'name' => string '***' (length=10)
  public 'picture' => string 'https://lh5.googleusercontent.com/***/photo.jpg' (length=92)
  public 'timezone' => null
  public 'verified_email' => boolean true
  protected 'data' => 
    array (size=0)
      empty
  protected 'processed' => 
    array (size=0)
      empty

推荐答案

有趣的用例.不幸的是,目前没有办法用一个令牌来做到这一点.原因是 youtube 频道在系统中由不同的对象(几乎像用户帐户)表示.并且 OAuth 授权可以属于 gmail 用户或 youtube 频道.当我们在请求中看到 youtube 范围时,我们会显示用户 ID 和用户可能拥有的任何频道的列表,并且批准用户选择其中一个.生成的令牌属于该对象/帐户.

Interesting use case. Unfortunately there is no way to do this with one token at this time. The reason is that youtube channel is represented by a different object (almost like a user account) in the system. And an OAuth grant can belong to either the gmail user or the youtube channel. When we see the youtube scope in the request, we show a list of user id and any channels that the user may have and the approving user selects one of them. And the resulting token belongs to that object/account.

我们正在构建与委托和渠道相关的新功能.例如一种可能的解决方案可能是在用户帐户上获取一个令牌,使您可以访问由该帐户管理的所有渠道上的 API.抱歉,目前没有预计到达时间.

We are building new features related to delegation and channels. e.g. One possible solution may be to get a single token on the user account that gives you access to APIs on all channels that are managed by that account. Sorry, there is no ETA at this time.

相关文章