Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

php - Google OAuth2 Login - Get YouTube nickname and real email address

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

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...

Requests performed by Google API PHP Client (https://github.com/google/google-api-php-client)


Examples

1. Right email / Wrong name:

Scopes:

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

Userinfo:

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

2. Wrong email / Right name:

Scopes:

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

Userinfo:

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
[...]

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


Code

$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;
}

Dump (with YT scope)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...