我正在尝试为拥有Google帐户的人添加登录选项到我的网站 . 我已经能够实现这个Facebook但在向Google获取用户帐户信息时遇到问题 .
我正在使用位于此处的Google PHP SDK: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中看到了类 Google_Service_IdentityToolkit
中名为 getAccountInfo()
的方法 . 但是,它需要的参数是 postBody
但我不知道如何获取/构建它 .
2 回答
这将返回一个Google_Service_Oauth2_Userinfoplus对象,其中包含您可能正在查找的信息:
$client
是Google_Client
的一个实例输出:
另请注意,您还需要请求
https://www.googleapis.com/auth/userinfo.profile
范围 .您应该能够通过构建Google_Service_OAuth2对象来获取此信息,将Google_Client作为参数传递,然后从那里获取用户信息 .