我是第一次实施Google Login API。它工作正常,但我只能检索电子邮件地址。我尝试了其他一些方法但没有成功。如何获取用户名等其他用户详细信息?
include('/../src/Google/autoload.php');
/* * **********************************************
ATTENTION: Fill in these values! Make sure
the redirect URI is to this page, e.g:
http://localhost:8080/user-example.php
* ********************************************** */
$client_id = 'bla-bla-bla';
$client_secret = 'bla-bla-bla';
$redirect_uri = 'bla-bla-bla/google_login';
/* * **********************************************
Make an API request on behalf of a user. In
this case we need to have a valid OAuth 2.0
token for the user, so we need to send them
through a login flow. To do this we need some
information from our API console project.
* ********************************************** */
if (isset($_GET['code'])) {
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
$client->authenticate($_GET['code']);
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$google_oauth = new Google_Service_Oauth2($client);
print_r($google_ouath);//give error Undefined variable: google_ouath
print_r($google_ouath->userinfo); //give error Undefined variable: google_ouath
print_r($google_ouath->userinfo>get());//give error Undefined variable: google_ouath
$google_account_email = $google_oauth->userinfo->get()->email; //works fine
//$google_account_getDisplayName = $google_ouath->userinfo->get()->DisplayName;
//$google_account_givenname = $google_ouath->userinfo->get()->givenName;
//$google_account_name = $google_ouath->userinfo->get()->name;
echo '<pre>';
print_r($google_account_email);
echo '</pre>';
exit;
答案 0 :(得分:0)
这是因为这里变量名称中的拼写错误:
print_r($google_ouath);//give error Undefined variable: google_ouath
print_r($google_ouath->userinfo); //give error Undefined variable: google_ouath
print_r($google_ouath->userinfo>get());//give error Undefined variable: google_ouath
变量名称google_ouath
应为google_oauth
,即a
和u
应切换,因此它变为:
print_r($google_oauth);
print_r($google_oauth->userinfo);
print_r($google_oauth->userinfo>get());
您还应该删除已弃用的长范围名称:
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
并使用简写和OpenID Connect格式:
$client->setScopes(array('email', 'profile'));