user3686568
user3686568

Reputation: 1

How to get user info when user is authenticated with Google Oauth2 and people.me

I am new to google api and php and using the code below to access user info after providing authentication and allowing app to access user info. It asks for login and permissions from user but after allowing I am still not able to receive anything the $_GET contains nothing..

    <?php
    set_include_path("includes/src/" . PATH_SEPARATOR . get_include_path());
    require_once 'Google/Client.php';
    require_once 'Google/Service/Plus.php';

$client_id = 'XXXXXXXXXXXXXXXXXX';
$client_secret = 'XXXXXXXXXXXXXXXXXX';
$redirect_uri = 'http://XXXXXXXXXXXXXXXXXX/test.php';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/userinfo.profile");
$client->addScope("https://www.googleapis.com/auth/userinfo.email");

$plus = new Google_Service_Plus($client);


print_r($_GET);

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 (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);
    $_SESSION['token'] = $client->getAccessToken();

} else {
    $authUrl = $client->createAuthUrl();
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Testing</title></head>
<body>
<?php if (isset($authUrl)) {
    print "<a class='login' href='$authUrl'>Login</a>";
} else {
    print "<a class='logout' href='test.php?logout'>Logout</a>";
}

echo "<br />";

if (isset($_SESSION['access_token'])) {
    $me = $plus->people->get("me");

    print "<br>ID: {$me['id']}\n<br>";
    print "Display Name: {$me['displayName']}\n<br>";
    print "Image Url: {$me['image']['url']}\n<br>";
    print "Url: {$me['url']}\n<br>";
    $name3 = $me['name']['givenName'];
    echo "Nombre: $name3 <br>"; 
    $correo = ($me['emails'][0]['value']);
    echo $correo;
    }
?>
</body>
</html>

Please let me know where I am going wrong.

Upvotes: 0

Views: 1005

Answers (1)

class
class

Reputation: 8681

First, you should try starting from the Google+ PHP Quickstart. You can use client-side authorization then authorize the server or just directly access the data you need. The older samples that don't use the Sign-In button may be out of date.

Next, I'm guessing that if you're not seeing the code be passed back to your site in the GET request, then something must be wrong with your configuration on the redirect URI. Make sure that you're pointing to the correct PHP page on your site both in the client configuration as well.

Finally, update the version of the Google+ PHP client library you are using. The version included in the quickstart should be the latest.

Upvotes: 1

Related Questions