php - How to get user details corectly with google api -
hi there have bug in code, tried user details need name, email code doesn't return email. how can fix ?
<?php include_once "templates/base.php"; session_start(); require_once ('src/google/autoload.php'); $client_id = '1061700181920-5i9r----something-----k3mogj328g9sed3.apps.googleusercontent.com'; $client_secret = 'zsfesn-----gsomething----pgtzce0uvm'; $redirect_uri = 'http://localhost/easy_b/shop/user-example.php'; $client = new google_client(); $client->setclientid($client_id); $client->setclientsecret($client_secret); $client->setredirecturi($redirect_uri); $client->addscope("https://www.googleapis.com/auth/plus.login"); $service = new google_service_oauth2($client); //logout if (isset($_request['logout'])) { unset($_session['access_token']); } if (isset($_get['code'])) { $client->authenticate($_get['code']); $_session['access_token'] = $client->getaccesstoken(); $redirect = 'http://' . $_server['http_host'] . $_server['php_self']; header('location: ' . filter_var($redirect, filter_sanitize_url)); } /************************************************ if have access token, can make requests, else generate authentication url. ************************************************/ if (isset($_session['access_token']) && $_session['access_token']) { $client->setaccesstoken($_session['access_token']); } else { $authurl = $client->createauthurl(); } /************************************************ if we're signed in , have request shorten url, create new url object, set unshortened url, , call 'insert' method on 'url' resource. note re-store access_token bundle, in case changed during request - main thing might happen here access token refreshed if application has offline access. ************************************************/ if ($client->getaccesstoken()) { $user = $service->userinfo->get($_post); echo '<pre>'; print_r($user); echo '</pre>'; $_session['access_token'] = $client->getaccesstoken(); } echo pageheader("user query - url shortener"); if (strpos($client_id, "googleusercontent") == false) { echo missingclientsecretswarning(); exit; } ?> <div class="box"> <div class="request"> <?php if (isset($authurl)) { echo "<a class='login' href='" . $authurl . "'>connect me!</a>"; } else { echo <<<end <a class='logout' href='?logout'>logout</a> end; } ?> </div> </div>
the code returns this
google_service_oauth2_userinfoplus object ( [internal_gapi_mappings:protected] => array ( [familyname] => family_name [givenname] => given_name [verifiedemail] => verified_email ) [email] => [familyname] => nvs [gender] => male [givenname] => jack [hd] => [id] => 100137049524582923700 [link] => https://plus.google.com/100137049524582923700 [locale] => en [name] => jack nvs (logromania) [picture] => https://lh6.googleusercontent.com/-vdyfv_rl0se/aaaaaaaaaai/aaaaaaaaado/wcrzfpemelk/photo.jpg [verifiedemail] => [modeldata:protected] => array ( [given_name] => jack [family_name] => nvs ) [processed:protected] => array ( )
after code correctly want show me email , name, insert them in database. but, how can proper data api?
thanks helping.
i added
$client->addscope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email");
and working.
Comments
Post a Comment