如何获取 Google Apps for Work 服务用户的基本个人资料信息,例如姓名和电子邮件

How to get basic profile information like name and email of users of Google Apps for Work services

我正在尝试使用 PHP 客户端库获取 Google Apps for Work 用户的基本个人资料信息,例如姓名和电子邮件。根据this question I can do that simply using people->get function with https://www.googleapis.com/auth/plus.login范围。

我试过了,这对@gmail.com 帐户很有效,但对 Google Apps for Work 无效 users.I 也尝试过使用 plus_domains 服务,但结果相同。我设法使用 gmail 范围获取电子邮件地址,但仍然无法获取用户名。

我还想提一下,Google Apps for Work 的用户可能没有 Google plus 管理员激活的服务,或者他们可能正在使用旧版免费版本,其中 Google附加服务不可用。

我使用 Google Apps for Education 使它正常工作,而没有使用 Google Plus API,仅使用 OAuth2 API。 这些是我添加的范围:

$client->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);

那你可以这样用:

$oauthService = new Google_Service_Oauth2($client);
$userInfo = $oauthService->userinfo_v2_me->get();
echo "User info:<br>Name: ".$userInfo->name
  ."<br>givenName: ".$userInfo->givenName
  ."<br>familyName: ".$userInfo->familyName
  ."<br>email: ".$userInfo->email;