9lessons programming blog
Loading Search
Monday, October 31, 2011

Login with Google Plus OAuth.

Few days back Google Plus has released OAuth application programming interface, right now they providing only user activities and circle data. 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.

Login with Google Plus OAuth.

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> ";
}
?>
Sponsored Links

Share this post

Comments
{ 33 comments }
Anil Kumar Panigrahi said...

Good one. I will try this ...

Anonymous said...

Get friends api?You are the best!!!

Anonymous said...

You should specify whether your going to save our data in your website or not, privacy option are really important please let us know whether you are going to save our data in your sql tables or not.

Srinivas Tamada said...

Demo not connected with database. That is why I'm just displaying insert SQL query.

TaneshaCodieKami said...

hey what if you get SSL error?? whats wrong?

Anonymous said...

What if you get "SSL error Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have"? what does that mean??

Sandeep Verma said...

Thanks Srinivas,

I really need this for scriptrr.com

:)

Anonymous said...

Hi Srinivas, Nice tutorial, is it possible to get the users email address? Usually i want to register users on my site with the provided data from Google in this example. For example, without the email address it's not possible to send a confirmation, activation email to the user who's logging with Google..

adnan said...

hey Srinivas it is working fine but the response comes in https:// which is generating error SSL connection error... Kindly explain how it can be fixed?

Burhan said...

Hi Srinivas

I love your tutorial. I have used few tutoral earlier and it worked fine. Recently I have tried Login using Twitter and Facebook but it did not work. Today I have downloaded your script and modified config file. I have uploloaded config file but did not get "Google Plus Login" link on mu page.

Can you tell me what could have done wrong?

Saqib Abbas said...

Hi Srinivas,
I am also facing the SSL Error.. Please guide me that how i can get Free SSL certificate for my website

Srinivas Tamada said...

@Saqib

You have to buy valid SSL certificate it around $1000 USD.

Anonymous said...

It won't cost you $1000 for a SSL. I got one from my web host for $29.99 a year. Here is a list of cheap ones and other hosts offer them for very little
http://www.cheapssls.com/?gclid=COSk6IH3l6wCFYXrKgodoUS5Ow

King said...

Srinivas.. as i know that your 9lessons is hosted on some Free WebHost then have you purchase SLL separate than your host??????????

Anonymous said...

is this free? & do it really authentic8

tsmotta said...

For those having problems with SSL, just set as false the "CURLOPT_SSL_VERIFYPEER" flag on the apiCurlIO.php file, method makeRequest. =)

JasonDavis said...

Hello could you explain to everyone the difference between this post and this one http://www.9lessons.info/2011/07/login-with-google-account.html

JasonDavis said...

Hello could you explain to everyone the differences between this method and the one here http://www.9lessons.info/2011/07/login-with-google-account.html and maybe which one is better to use?

David said...

Its really useful tutorial.Thank you.

Leonardo said...

Cool I will use this for a project...

Palani said...

Hi Srinivas,

I have seen the live demo (http://www.9lessons.info/2011/10/login-with-google-plus-oauth.html) . Is it any possibility to avoid the Login screen (send EmailId and Passwd through PHP)?

If yes, please let me know the steps.

Thanks in Advance
Palani G.

Netbook or Smartphone said...

Finally, Googl eis trying to overtake one by one, first with the Google Plus, Next Hangouts video chat, next came location based status updates, next came Google Plus Pages for business and people and now it came with the OAuth login systems for websites. Good going, but it seems very slow in the field of Social Network. I heard that Microsoft is also planning a "Socl" Network launch soon.

Manolo said...

Hi Sri
I wonder if is possible implement a login with the data of a specific "google group" members ?

bye

Anonymous said...

what means query limits/day is 1000 ? 100 users login 10 times = 1000 ? if is so i better oauth 1.0

khaja moin said...

Hi,
Srinivas excellent work.

rashid said...

google should not exit their existing api system..becoz most of the poeples are using it

Surya Adi Saputra said...

i really like your articles, this is make me have more idea. thanks.

suresh veduvaka said...

too good piece of information, I had come to know about your site from my friend sajid, bangalore,i have read atleast 11 posts of yours by now, and let me tell you, your web-page gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new post, once again hats off to you! Thanks a lot once again, Regards, googleplus

abhishek said...

Very good.

Ashish said...

Seems great!
But why the name and any other info about me is coming, when I am logging-in with Google Plus. you must show name or any any info which can be accessed after logging-in through Google Plus.
Thanks for the very useful info.

SHIV said...

Thanks Srinivas,
I'm going to use it a website which i'm going to create.

Manuel said...

Fatal error: Uncaught exception 'apiIOException' with message 'HTTP Error: (0) couldn't connect to host' in /home/.sites/6/site1542/web/subdomains/lab/gplus/src/io/apiCurlIO.php:120 Stack trace: #0 /home/.sites/6/site1542/web/subdomains/lab/gplus/src/auth/apiOAuth2.php(93): apiCurlIO->makeRequest(Object(apiHttpRequest)) #1 /home/.sites/6/site1542/web/subdomains/user/lab/src/apiClient.php(131): apiOAuth2->authenticate(Array) #2 /home/.sites/6/site1542/web/subdomains/lab/gplus/gplus_login.php(32): apiClient->authenticate() #3 /home/.sites/6/site1542/web/subdomains/lab/gplus/index.php(9): include('/home/.sites/6/...') #4 {main} thrown in /home/.sites/6/site1542/web/subdomains/lab/gplus/src/io/apiCurlIO.php on line 120

Mangalamurthy Bhat said...

Please help
i am getting where i am wrong

Fatal error: Uncaught exception 'apiAuthException' with message 'Invalid token format' in /home/burnin27/public_html/sites/indiatidy/core/oauth2callback/src/auth/apiOAuth2.php:108 Stack trace: #0 /home/burnin27/public_html/sites/indiatidy/core/oauth2callback/src/auth/apiOAuth2.php(74): apiOAuth2->setAccessToken('{? "access_tok...') #1 /home/burnin27/public_html/sites/indiatidy/core/oauth2callback/src/apiClient.php(130): apiOAuth2->authenticate(Array) #2 /home/burnin27/public_html/sites/indiatidy/core/oauth2callback/gplus_login.php(32): apiClient->authenticate() #3 /home/burnin27/public_html/sites/indiatidy/core/oauth2callback/index.php(9): include('/home/burnin27/...') #4 {main} thrown in /home/burnin27/public_html/sites/indiatidy/core/oauth2callback/src/auth/apiOAuth2.php on line 108

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension