Wednesday, 18 March 2015

Create Login With Google Plus in Your Website With PHP

Create Login With Google Plus in Your Website With PHP

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.
  infotuts-google-login
Live Demo       Download
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.
google_plus_login
2> Go to APIs and Auth and then under credentials tab. Click on create new client ID as shown below.
Google_login_1
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.
google_api_2-_infotuts
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.
Google_login_api-_3
4> Now you have to set Consent screen.
Google_login_4
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.
google_login_5
Once all is done, we must get on and code our login system using API. First of all create two fileapi.php and login.php. In our api.php we will start user session and will ask user to login.
1</p>
2<?php
3require_once 'lib/src/Google_Client.php';
4require 'lib/src/contrib/Google_Oauth2Service.php';
5require_once 'lib/src/contrib/Google_PlusService.php';
6session_start();
7 $api new Google_Client();
8 $api->setApplicationName("InfoTuts"); // Set Application name
9 $api->setClientId('########################################'); // Set Client ID
10 $api->setClientSecret('####################################'); //Set client Secret
11 $api->setAccessType('online'); // Access method
13 $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
14 $service new Google_PlusService($api);
15 $URI $api->createAuthUrl();
16?>
17<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:
1</p>
2 
3 function login(){
4 session_start();
5 $this->lib_include();
6 $api new Google_Client();
7 $api->setApplicationName("InfoTuts");
8 $api->setClientId('##########################################');
9 $api->setClientSecret('#######################################');
10 $api->setAccessType('online');
13 $service new Google_PlusService($api);
14 $oauth2 new Google_Oauth2Service($api);
15 $api->authenticate();
16 $_SESSION['token'] = $api->getAccessToken();
17 if (isset($_SESSION['token'])) {
18 $set_asess_token $api->setAccessToken($_SESSION['token']);
19 }
20 if ($api->getAccessToken()) {
21 $data $service->people->get('me');
22 $user_data $oauth2->userinfo->get(); ?>
23<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.

No comments:

Post a Comment