Get user information from Google OAuth PHP API -
i user information name, family name, email address, image , etc.
after login website google account, i've used following php code:
<?php ########## google settings.. client id, client secret ############# // fill these fieds keys $google_client_id = '............'; $google_client_secret = '...............'; $google_redirect_url = '.......................'; $google_developer_key = '............'; ########## mysql details (replace yours) ############# // filled these fields data $db_username = "*******"; //database username $db_password = "*******"; //database password $hostname = "*******"; //mysql hostname $db_name = '**********'; //database name ################################################################### //include google api files require_once 'src/google_client.php'; require_once 'src/contrib/google_oauth2service.php'; //start session session_start(); $gclient = new google_client(); $gclient->setapplicationname('login techsa.ir'); $gclient->setclientid($google_client_id); $gclient->setclientsecret($google_client_secret); $gclient->setredirecturi($google_redirect_url); $gclient->setdeveloperkey($google_developer_key); $google_oauthv2 = new google_oauth2service($gclient); //if user wish log out, unset session variable if (isset($_request['reset'])) { unset($_session['token']); $gclient->revoketoken(); header('location: ' . filter_var($google_redirect_url, filter_sanitize_url)); } if (isset($_get['code'])) { $gclient->authenticate($_get['code']); $_session['token'] = $gclient->getaccesstoken(); header('location: ' . filter_var($google_redirect_url, filter_sanitize_url)); return; } if (isset($_session['token'])) { $gclient->setaccesstoken($_session['token']); } if ($gclient->getaccesstoken()) { //get user details if user logged in $user = $google_oauthv2->userinfo->get(); $user_id = $user['id']; $user_name = filter_var($user['name'], filter_sanitize_special_chars); $email = filter_var($user['email'], filter_sanitize_email); $profile_url = filter_var($user['link'], filter_validate_url); $profile_image_url = filter_var($user['picture'], filter_validate_url); $personmarkup = "$email<div><img src='$profile_image_url?sz=50'></div>"; $_session['token'] = $gclient->getaccesstoken(); } else { //get google login url $authurl = $gclient->createauthurl(); } //html page start echo '<html xmlns="http://www.w3.org/1999/xhtml">'; echo '<head>'; echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'; echo '<title>login google</title>'; echo '</head>'; echo '<body>'; echo '<h1>login google</h1>'; if(isset($authurl)) //user not logged in, show login button { echo '<a class="login" href="'.$authurl.'"><img src="images/google-login-button.png" /></a>'; } else // user logged in { /* connect mysql */ $connecdb = mysql_connect($hostname, $db_username, $db_password)or die("unable connect mysql"); mysql_select_db($db_name,$connecdb); //compare user id in our database $result = mysql_query("select count(g_id) social_users g_id=$user_id"); if($result === false) { die(mysql_error()); //result false show db error , exit. } $usercount = mysql_fetch_array($result); if($usercount[0]) //user id exist in database { echo 'welcome '.$user_name.'!'; }else{ //user new echo 'hello! '.$user_name.', registering!'; @mysql_query("insert social_users (g_id, g_name, g_email, g_link, g_image, created_date) values ($user_id, '$user_name','$email','$profile_url','$profile_image_url', now())"); } echo '<br /><a href="'.$profile_url.'" target="_blank"><img src="'.$profile_image_url.'?sz=50" /></a>'; echo '<br /><a class="logout" href="?reset=1">logout</a>'; //list user details echo '<pre>'; print_r($user); echo '</pre>'; } echo '</body></html>'; ?>
i have table in database named social_users
. user gives permission, table empty.
the $google_developer_key
not needed in case.
here how fetch user information nowadays using google-api-php-client library:
<?php require_once('google-api-php-client-1.1.7/src/google/autoload.php'); const title = 'my amazing app'; const redirect = 'https://example.com/myapp/'; session_start(); $client = new google_client(); $client->setapplicationname(title); $client->setclientid('replace_me.apps.googleusercontent.com'); $client->setclientsecret('replace_me'); $client->setredirecturi(redirect); $client->setscopes(array(google_service_plus::plus_me)); $plus = new google_service_plus($client); if (isset($_request['logout'])) { unset($_session['access_token']); } if (isset($_get['code'])) { if (strval($_session['state']) !== strval($_get['state'])) { error_log('the session state did not match.'); exit(1); } $client->authenticate($_get['code']); $_session['access_token'] = $client->getaccesstoken(); header('location: ' . redirect); } if (isset($_session['access_token'])) { $client->setaccesstoken($_session['access_token']); } if ($client->getaccesstoken() && !$client->isaccesstokenexpired()) { try { $me = $plus->people->get('me'); $body = '<pre>' . print_r($me, true) . '</pre>'; } catch (google_exception $e) { error_log($e); $body = htmlspecialchars($e->getmessage()); } # access token may have been updated lazily $_session['access_token'] = $client->getaccesstoken(); } else { $state = mt_rand(); $client->setstate($state); $_session['state'] = $state; $body = sprintf('<p><a href="%s">login</a></p>', $client->createauthurl()); } ?> <!doctype html> <html> <head> <title><?= title ?></title> </head> <body> <?= $body ?> <p><a href="<?= redirect ?>?logout">logout</a></p> </body> </html>
do not forget -
- get web client id , secret @ google api console
- authorize
https://example.com/myapp/
@ same place
you can find more examples @ youtube github.
Comments
Post a Comment