So you want to allow users to login into your website using their gmail credentials? You have seen various websites that allow their users to login in their websites using gmail, facebook, linked in, Microsoft, git hub credentials. It’s time to integrate it in your website. We will cover all the login system in our posts one by one and this one is dedicated to create Google Plus login for your website with PHP using OAuth2. Google offers many APIs like Google Maps, translate API, Analytics ApI etc. Today we will use its Google Plus API so lets proceed with our tutorial.
Google offers API that uses OAuth2 for authorization and allows user to use their Google Plus credential to login in any website or application. First of all you need to get a Google client secret and key. Lets start our step step guide:
1> Login to Google API Console. Go to APIs and you will have to turn on Google Plus API.
2> Go to APIs and Auth and then under credentials tab. Click on create new client ID as shown below.
3> Now when you will have to enter your website path and the file path (redirect URI) to get your new client ID. You will need to enter these client ID and secret in your code later.
Now your client ID and client secret will be generated. If in above step you fill anything wrong then it can be edited and changed later.
4> Now you have to set Consent screen.
5> In consent screen if you have entered Google Plus page path then you will have to approve connection. So Approve it by clicking on Plus page management and you are good to go.
Once all is done, we must get on and code our login system using API. First of all create two file api.php and login.php. In our api.php we will start user session and will ask user to login.
</p> <?php require_once 'lib/src/Google_Client.php'; require 'lib/src/contrib/Google_Oauth2Service.php'; require_once 'lib/src/contrib/Google_PlusService.php'; session_start(); $api = new Google_Client(); $api->setApplicationName("InfoTuts"); // Set Application name $api->setClientId('########################################'); // Set Client ID $api->setClientSecret('####################################'); //Set client Secret $api->setAccessType('online'); // Access method $api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile')); $api->setRedirectUri('http://www.infotuts.com/demo/googlelogin/login.php'); // Enter your file path (Redirect Uri) that you have set to get client ID in API console $service = new Google_PlusService($api); $URI = $api->createAuthUrl(); ?> <p style="text-align: justify;">
Now in our login.php file we need to authenticate user and will display his details if he enters his correct Google plus credentials. We have written a login() function to authenticate user and store his details in an array:
</p> function login(){ session_start(); $this->lib_include(); $api = new Google_Client(); $api->setApplicationName("InfoTuts"); $api->setClientId('##########################################'); $api->setClientSecret('#######################################'); $api->setAccessType('online'); $api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile')); $api->setRedirectUri('http://www.infotuts.com/demo/googlelogin/login.php'); $service = new Google_PlusService($api); $oauth2 = new Google_Oauth2Service($api); $api->authenticate(); $_SESSION['token'] = $api->getAccessToken(); if (isset($_SESSION['token'])) { $set_asess_token = $api->setAccessToken($_SESSION['token']); } if ($api->getAccessToken()) { $data = $service->people->get('me'); $user_data = $oauth2->userinfo->get(); ?> <p style="text-align: justify;">
That’s all 🙂 yes I have put the main code of both the files. You can download the working code and replace client secret, client ID and redirect URI and deploy it in your server.  You can destroy session on user log out. Its good to integrate this in your website as most of the users already have Google account so they do not need to create a new account. We can do a lot more with Googel API’s and I will try to cover as much as I can. Let me know in comments if you get stuck somewhere in implementing this in your web project.
Hello,
Very good tutorial, i want to use in my site, will you please explain how to redirect to login when try to access index.html (or .php) if they are not logged in.
Reply
thanks
Hello Chirag,
Glad that you liked this tutorial. If user is not logged in and accessing any webpage that is supposed to be accessed after login you can check the session value if its not set then you can redirect him to the page you want.
Here is a sample code to help you understand what I mean.
Pingback: InfoTuts.com: Create Login With Google Plus in Your Website With PHP | facebooklikes
Pingback: Login with Facebook in Your Website Using Facbook SDK for PHP InfoTuts
sorry! the download is not working. i have subscribed through my email id and got a email too. but still error is showing up
can’t download code.
displayed code is not complete.
want to download.
please fix! 🙂 thank you
Please download your file. Email list is updated every 12 hours
thanks
Its really helpful for me, I am not PHP developer still I implement this facebook login very easily. Its very friendly tutorial
Thanks once again, its give me a boost in my energy
i am geetting an 500 internal server error when i a trying to run the code given above.
Hi,
Please make sure your all paths are correct and you user not using IP to access if you are in localserver. Please direct your ID to a domain name.
use http://localhost. to access local server.
Thanks:
InfoTuts
VERY VERY BED
I have subscribed through mentioned mail-id but download link is not coming. Not able to download.
I have waited 24 hours and still can’t download the package. Could you help me please? Seems so interesting.
Hello Sanjeev, I suscribed to download the script but one day later, it still saying I need to subscribe to download it… Any chance you could send me the files to my email ? Thank you so much !!!
Hi Charles,
Please download the file now.
Hi i gone through you code its really simple and good. I would like to know , where i should include the above code or file so that those will be executed. Do i have to link those to google+ image, which of the above file need to be connected with the image of google+, Please explain need very urgently for my web site….
hello i have one problem when i log in google plus auth login successfully but logout button not display………….
hi,
i am not able to download the code. Its been more that 3 days subscribed on the website.
I need to integrate the google plus API onto the santoor.co.in webesite.
Kindly provide the code or download link if possible.
Regards
Vipin
Hi Vipin,
Please try downloading your code again, you should be able to download the code.
Thanks:
Sanjeev
hello
i subscribe but doanloadd is not working…
Alex
Hi,
Please try downloading now.
Thanks:
Sanjeev
Sir,from where i can get DEVELOPERS KEY?
Pingback: Login with Twitter using PHP - InfoTuts
hey dude how can I retrieve the email from the data I receive from the App?
subscribed. But not able to get the code even if i enter email and click on download button.Please reply….
You should be able to download the code now.
I am to much confused with the what is the consumer key & how its gotted
I found following error. Sir please help me to sort out these eroors.
401. That’s an error.
Error: invalid_client
The OAuth client was not found.