Login with Google Plus OAuth.
Wall Script
Follow Me:
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> ";
}
?>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


63 comments:

  1. Get friends api?You are the best!!!

    ReplyDelete
  2. 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.

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

    ReplyDelete
  4. hey what if you get SSL error?? whats wrong?

    ReplyDelete
  5. 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??

    ReplyDelete
  6. Thanks Srinivas,

    I really need this for scriptrr.com

    :)

    ReplyDelete
  7. 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..

    ReplyDelete
  8. 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?

    ReplyDelete
  9. 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?

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

    ReplyDelete
  11. @Saqib

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

    ReplyDelete
  12. 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

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

    ReplyDelete
  14. is this free? & do it really authentic8

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

    ReplyDelete
  16. 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

    ReplyDelete
  17. 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?

    ReplyDelete
  18. Its really useful tutorial.Thank you.

    ReplyDelete
  19. Cool I will use this for a project...

    ReplyDelete
  20. 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.

    ReplyDelete
  21. 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.

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

    bye

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

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

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

    ReplyDelete
  26. 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.

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

    ReplyDelete
  28. 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

    ReplyDelete
  29. 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

    ReplyDelete
  30. The Demo is not working. I checked 2 months ago or so and it did. So I guess there is a update somewhere bugging the code.

    I downloaded the code and it does not work either. Can someone confirm this?

    ReplyDelete
  31. I dowloaded this on my server and I got the same problem that demo has... it get stuck to the index.php?code=.... Srinivas watch this.

    Good piece of work though. It gives an idea of what google api are working.

    Thanks.

    ReplyDelete
  32. Thanks Srinivas,

    I have tested it and works fine.I just want to know that can I add any person to my circle or can I send friend request to any person by id or email using gplus api.

    Please tell me. I did not find any gplus api for friend invitation.

    Thanks in Advanced

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

    ReplyDelete
  34. Vinay Bhatt from Narola InfotechMarch 21, 2012 at 8:30 AM

    Google Plus also the same problem repeated after getting info from G+ it does not get logged out.Even here there is no logout option too so can u pls check it out for log out to G+.
    Thanks

    ReplyDelete
  35. I am not able to fetch email id and mobile number from google plus account. Please help me.

    ReplyDelete
  36. How can i fetch email id from there.

    ReplyDelete
  37. Fatal error: Uncaught exception 'apiAuthException' with message 'Invalid token format' in /opt/lampp/htdocs/qibo/src/auth/apiOAuth2.php:108 Stack trace: #0 /opt/lampp/htdocs/qibo/src/auth/apiOAuth2.php(74): apiOAuth2->setAccessToken('{? "access_tok...') #1 /opt/lampp/htdocs/qibo/src/apiClient.php(130): apiOAuth2->authenticate(Array) #2 /opt/lampp/htdocs/qibo/login.php(12): apiClient->authenticate() #3 {main} thrown in /opt/lampp/htdocs/qibo/src/auth/apiOAuth2.php on line 108
    not work for me

    ReplyDelete
  38. my gplusdata variable is always empty. I don't know to do

    ReplyDelete
  39. Is this updated to the Oauth 2.0 ??

    ReplyDelete
  40. Please help
    i am getting error when trying to integrate this, I am not able to retrieve $_SESSION['gplusdata'] and also getting the below errors -


    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /nfs/c07/h03/mnt/104386/domains/reversethought.com/html/clients/india_is_photography/home.php:8) in /nfs/c07/h03/mnt/104386/domains/reversethought.com/html/clients/india_is_photography/home.php on line 9

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /nfs/c07/h03/mnt/104386/domains/reversethought.com/html/clients/india_is_photography/home.php:8) in /nfs/c07/h03/mnt/104386/domains/reversethought.com/html/clients/india_is_photography/home.php on line 9

    Warning: Cannot modify header information - headers already sent by (output started at /nfs/c07/h03/mnt/104386/domains/reversethought.com/html/clients/india_is_photography/home.php:8) in /nfs/c07/h03/mnt/104386/domains/reversethought.com/html/clients/india_is_photography/home.php on line 13
    SQL Query : insert into users(oauth_provider, username,gplusid, gender, location) values("Google"," "," "," ","")';

    Thanks

    ReplyDelete
  41. These are warning.Not errors.So don't care.
    Add error_reporting(0);
    in every page.
    more info:
    http://faq.pctrickers.com/php-import-email-addresses-from-gmail-account/

    ReplyDelete
  42. I got this error: Please help me

    Fatal error: Uncaught exception 'Exception' with message 'The Google PHP API Library needs the CURL PHP extension' in C:\xampp\htdocs\gplus123\gplus\src\apiClient.php:20 Stack trace: #0 C:\xampp\htdocs\gplus123\gplus\gplus_login.php(17): require_once() #1 C:\xampp\htdocs\gplus123\gplus\index.php(9): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\gplus123\gplus\src\apiClient.php on line 20

    ReplyDelete
  43. I got this error.please help me..

    Fatal error: Uncaught exception 'apiAuthException' with message 'Error fetching OAuth2 access token, message: 'unauthorized_client'' in /home/kavin623/public_html/demo/aves/class/google/google-api-php-client/src/auth/apiOAuth2.php:104 Stack trace: #0 /home/kavin623/public_html/demo/aves/class/google/google-api-php-client/src/apiClient.php(131): apiOAuth2->authenticate(Array) #1 /home/kavin623/public_html/demo/aves/class/google/google-plus-access.php(25): apiClient->authenticate() #2 /home/kavin623/public_html/demo/aves/profile_page.php(7): include('/home/kavin623/...') #3 {main} thrown in /home/kavin623/public_html/demo/aves/class/google/google-api-php-client/src/auth/apiOAuth2.php on line 104

    ReplyDelete
  44. hiii srinivas,
    IS this process works on wordpress blogg too.
    can up please tell me

    ReplyDelete
  45. how can i get list frient send messenger for friend my. like invite function of facebook

    ReplyDelete
  46. Hi,
    I tried your Demo. But,my expectation is to implement in native blackberry mobile application.

    Can you suggest me some useful tips/metheds.

    ReplyDelete
  47. how does this work?
    where is config.php called?
    where are the oauth2 client id and client secret used?

    ReplyDelete
  48. Good one Srinivas ! but how to retrieve the email id using this API ?

    ReplyDelete
  49. How to use it over multiple subdomains.

    ReplyDelete
  50. hi ,

    if we want google plus to operate on both http and https how can we achieve this

    ReplyDelete
  51. Nice script but too BIG :( . 200kb with many files , is there a very simple script with just 2 or 3 files ??

    ReplyDelete
  52. I fixed the "Uncaught exception" error by putting ob_start(); on home.php and index.php. You insert this code on top of these pages to avoid the session issue.

    Altough I still cannot sign out on G+. Even in your demo it is not possible to sign out. After clicking sign out you enter home.php again and you will see you are still logged. Can you please fix this? Thanks for the tuto.

    ReplyDelete
  53. I am getting error. I have changed CURLOPT_SSL_VERIFYPEER to false but still getting following 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.

    ReplyDelete
  54. I dont have a website, how can i test with my localhost ? Please tell me how

    ReplyDelete
  55. I got this error.please help me..


    Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling GET https://www.googleapis.com/plus/v1/people/me?key=AIzaSyDYiRbf6Cn-Gz-pxFsAInvulRDTe_cVp4Q: (403) There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.' in C:\xampp\htdocs\gplus\src\io\apiREST.php:86 Stack trace: #0 C:\xampp\htdocs\gplus\src\io\apiREST.php(56): apiREST::decodeHttpResponse(Object(apiHttpRequest)) #1 C:\xampp\htdocs\gplus\src\service\apiServiceResource.php(187): apiREST::execute(Object(apiServiceRequest)) #2 C:\xampp\htdocs\gplus\src\contrib\apiPlusService.php(207): apiServiceResource->__call('get', Array) #3 C:\xampp\htdocs\gplus\gplus_login.php(42): PeopleServiceResource->get('me') #4 C:\xampp\htdocs\gplus\index.php(10): include('C:\\xampp\\htdocs...') #5 {main} thrown in C:\xampp\htdocs\gplus\src\io\apiREST.php on line 86

    ReplyDelete
  56. undefined indexes on site
    Relationship:

    Notice: Undefined index: relationshipStatus in D:\Xampp\htdocs\google_login\home.php on line 26



    Location:

    Notice: Undefined index: placesLived in D:\Xampp\htdocs\google_login\home.php on line 27



    Tagline:

    Notice: Undefined index: tagline in D:\Xampp\htdocs\google_login\home.php on line 28

    ReplyDelete

Make in India