0
我首次實施Google Login API。它工作正常,但我只能檢索電子郵件地址。我嘗試了其他方法,但沒有成功。我如何獲得其他用戶詳細信息,如用戶名?使用OAuth檢索Google用戶詳細信息
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;