谷歌应用SSO -姓氏和名字与OAuth 2不使用Google+


Google Apps SSO - First and Last Name with OAuth 2 without using Google+

我正在尝试使用OAuth 2实现Google Apps SSO。为此,我使用了Google PHP客户端库。在身份验证期间,我将范围设置为'email''profile':

$this->client->setScopes('email', 'profile');

我能够使用verifyIdToken获得电子邮件地址,以及使用Google Plus的名字和姓氏。

if ($this->client->getAccessToken()) {
    $tokenData = $this->client->verifyIdToken()->getAttributes();
    // Email: $tokenData['payload']['email']
    $plus = new 'Google_Service_Plus($this->client);
    $me = $plus->people->get('me');
    // First name: $me['modelData']['name']['givenName']
    // Last name: $me['modelData']['name']['familyName']
}

我面临的问题是,许多用户使用谷歌应用程序没有在谷歌+的个人资料。我的问题是:我如何得到验证用户的名字和姓氏没有使用Google Plus服务?

注意:我已经尝试用正确的访问令牌发布到https://www.googleapis.com/oauth2/v1/userinfo,但这很快就会被弃用,而且,它还返回空的名字和姓氏的用户谁没有Google+配置文件。

我最终使用了库中包含的OAuth 2.0 Service。

require_once 'Google/Service/Oauth2.php';
$oAuth2 = new 'Google_Service_Oauth2($this->client);
$oAttr = $oAuth2->userinfo->get();

名字是$oAttr['givenName'],姓氏是$oAttr['familyName']

我还必须将范围更改为"openid email profile":

$this->client->setScopes('openid email profile');