Kshitiz Shankar Kshitiz Shankar - 1 year ago 295
PHP Question

Get Userinfo from Google OAuth 2.0 PHP API

I am trying to use Google Oauth API to get userinfo.
It works perfectly for Google Plus API but I am trying to create a backup in case the user doesn't have google plus account.
The authentication process is correct and I even get the $userinfo object but how exactly do I access the properties. I tried $userinfo->get() but it only return the id of the user.

Am I doing something wrong? Here is the code that I am using...

require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_Oauth2Service.php';


$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$plus = new Google_Oauth2Service($client);

if (isset($_REQUEST['logout'])) {

if (isset($_GET['code'])) {
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

if (isset($_SESSION['access_token'])) {

if ($client->getAccessToken())
$userinfo = $plus->userinfo;

} else
$authUrl = $client->createAuthUrl();
<!doctype html>
<meta charset="utf-8">
<link rel='stylesheet' href='style.css' />
<header><h1>Google+ Sample App</h1></header>
<div class="box">

<?php if(isset($personMarkup)): ?>
<div class="me"><?php print $personMarkup ?></div>
<?php endif ?>

if(isset($authUrl)) {
print "<a class='login' href='$authUrl'>Connect Me!</a>";
} else {
print "<a class='logout' href='?logout'>Logout</a>";


Was missing Scopes--Added


works now...

Answer Source

Was missing scopes


Works like a charm now!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download