我正在尝试为拥有 Google 帐户的用户添加登录选项到我的网站。我已经能够实现这个 Facebook,但在使用 Google 获取用户帐户信息时遇到问题。
我正在使用位于此处的 Google PHP SDK:https://github.com/google/google-api-php-client https://github.com/google/google-api-php-client
$client = new Google_Client();
$client->setClientId($this->ci->config->item('client_id', 'google'));
$client->setClientSecret($this->ci->config->item('client_secret', 'google'));
$client->setRedirectUri($this->ci->config->item('callback_uri', 'google'));
$client->addScope('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login');
$this->client->createAuthUrl();
但现在我如何访问用户的电子邮件地址和其他基本信息呢?
我在 Google PHP SDK 中看到一个名为getAccountInfo()
在课堂里Google_Service_IdentityToolkit
。然而,它需要的参数是postBody
但我不知道如何获取/构建它。
这将返回一个 Google_Service_Oauth2_Userinfoplus 对象,其中包含您可能正在查找的信息:
$oauth2 = new \Google_Service_Oauth2($client);
$userInfo = $oauth2->userinfo->get();
print_r($userInfo);
Where $client
是一个实例Google_Client
Outputs:
Google_Service_Oauth2_Userinfoplus Object
(
[internal_gapi_mappings:protected] => Array
(
[familyName] => family_name
[givenName] => given_name
[verifiedEmail] => verified_email
)
[email] =>
[familyName] =>
[gender] =>
[givenName] =>
[hd] =>
[id] => 123456
[link] => https://plus.google.com/123456
[locale] => en-GB
[name] => someguy
[picture] => https://lh3.googleusercontent.com/-q1Smh9d8d0g/AAAAAAAAAAM/AAAAAAAAAAA/3YaY0XeTIPc/photo.jpg
[verifiedEmail] =>
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
另请注意,您需要请求https://www.googleapis.com/auth/userinfo.profile
范围也是如此。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)