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


Google OAuth2 Login - Get YouTube nickname and real email address

有没有办法将用户的youtube昵称和他的REAL(*@gmail.com)电子邮件地址放在一起?

每当我要求谷歌用"youtube.readonly"范围对用户进行身份验证时,电子邮件地址就会更改为"*@pages.plusgoogle.com"。但当我去掉youtube范围时,我不会得到youtube昵称。。。

由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的名称(什么需要重新身份验证)?


代码

$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和用户可能拥有的任何频道的列表,批准用户会选择其中一个。生成的令牌属于该对象/帐户。

我们正在构建与授权和渠道有关的新功能。例如,一种可能的解决方案是在用户帐户上获得一个令牌,使您可以访问该帐户管理的所有频道上的API。对不起,目前没有预计到达时间。

相关文章: