retrieve Google user details with OAuth -
i implementing google login api first time. works fine able retrieve email addresses only. have tried other ways no success. how can other user details username?
include('/../src/google/autoload.php'); /* * ********************************************** attention: fill in these values! make sure redirect uri 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 api request on behalf of user. in case need have valid oauth 2.0 token user, need send them through login flow. need information 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;
that's because of spelling error in variable names here:
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
variable name google_ouath
should google_oauth
i.e. a
, u
should switched, becomes:
print_r($google_oauth); print_r($google_oauth->userinfo); print_r($google_oauth->userinfo>get());
you should rid of deprecated long scope names:
$client->setscopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
and use short-hand , openid connect compliant form:
$client->setscopes(array('email', 'profile'));
Comments
Post a Comment