Tuesday 1 November 2011

Login with Google Plus OAuth.

I had implemented a simple login system called User authentication with Google Plus data. Try this it’s almost like twitter login system, I hope in future Google + going to release some more options.



Download Script              Live Demo

 


Step 1

Add or register your domain at click here.
Importing GMail Contacts Google OAuth Connect with PHP.

Step 2

Verify your domain ownership with HTML file upload or including META tag.
Importing GMail Contacts Google OAuth Connect with PHP.

Step 3

Google will provide you OAuth consumer key and OAuth consumer secret key.
Importing GMail Contacts Google OAuth Connect with PHP.

Step 4

Create client ID OAuth Console here.
Login with Google Plus Oauth

Step 5

Create client ID.
Login with Google Plus OAuth.

Step 6

Here the application OAuth client ID and client secret.
Login with Google Plus OAuth.

Config.php
Here you have to configure application OAuth keys and Consumer keys.
// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console Step 6 keys
'oauth2_client_id' => 'App Client ID',
'oauth2_client_secret' => 'App Client Secret',
'oauth2_redirect_uri' => 'http://yoursite.com/gplus/index.php',// OAuth1 Settings Step 3  keys.
'oauth_consumer_key' => 'OAuth Consumer Key',
'oauth_consumer_secret' => 'OAuth Consumer Secret',


gplus_login.php
Google plus login system.
<?php
require_once 'src/apiClient.php';
require_once 'src/contrib/apiPlusService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("9lessons Google+ Login Application");
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);
if (isset($_REQUEST['logout']))
{
unset($_SESSION['access_token']);
}if (isset($_GET['code']))
{
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token']))
{
$client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken())
{
$me = $plus->people->get('me');
$_SESSION['access_token'] = $client->getAccessToken();
}
else
$authUrl = $client->createAuthUrl();

if(isset($me))
{
$_SESSION['gplusdata']=$me;
header("location: home.php");
}

if(isset($authUrl))
print "<a class='login' href='$authUrl'>Google Plus Login </a>";
else
print "<a class='logout' href='index.php?logout'>Logout</a>";
?>


home.php
Contains PHP code inserting Google plus session details into users table.
<?php
session_start();
if (!isset($_SESSION['gplusdata']))
{
// Redirection to home page
header("location: index.php");
}
else
{
$me=$_SESSION['gplusdata'];
echo "<img src='$me['image']['url']; ' />";
echo "Name: $me['displayName']; ";
echo "Gplus Id:  $me['id']";
echo "Male: $me['gender']";
echo "Relationship: $me['relationshipStatus']";
echo "Location: $me['placesLived'][0]['value']";
echo "Tagline: $me['tagline'];
print "<a class='logout' href='index.php?logout'>Logout</a> ";
}
?>

No comments:

Post a Comment