Login with Google Account using Oauth 2.0

Are you trying to connect to one of the Google Apis using Oauth 2.0? Would you like to allow to request a user to request their Google+ Account? In this tutorial i will show you how you can implement Google+ OAuth API using PHP.

Register your App

I have already told in my previous tutorial about how to register your app on Google Developers Console.Follow the article to register your app.
After registering your app follow these steps:

Step 1:Select your app and Go to APIs and Oauth> APIs and enable the Google+ API

Allow Google+ API

Step 2: Now Create new Client ID

Create Client ID

Step 6:Now select your Keys

Your Keys

Google API PHP client library

The newest version of Google API Client library can be found on github. Go to google-api-php-client, Download this file and copy the entire src/Google directory to the directory of your application.

OAuth 2.0

There are few steps which you have to follow.
Step 1:Include the follow files to you application

require_once 'google-api-php-client-master/src/Google/Client.php';
require_once 'google-api-php-client-master/src/Google/Service/Oauth2.php';
require_once 'google-api-php-client-master/src/Google/Service/Plus.php';

Step 3:Initialize the Google OAuth resource in your application.

$client = new Google_Client();

Step 3:Initialize the Google App in your application.

//Your Client ID
$client->setClientId("Your Client ID");
//Your Client Secret
$client->setClientSecret("Your Client Secret");
//Your Google OAuth Redirect URI
$client->setRedirectUri("Your Redirect URI");

Step 4: Start a session to store the token returned from Google OAuth.


Step 5: Set Scopes.
We use setScopes() function to tell Google what we want to access from the user. In our case we need to access Google+ data. So we provide scope specific to Google+ as argument. You can optionally include scopes for more Google services if you wish.


You can add more than one scope by adding a space in your scope.

$client->setScopes("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");

Step 6: Create a Google OAuth Login Url.
We use createAuthUrl() function to create Google OAuth Login URL

  //Create Google Login URL
  $authUrl = $client->createAuthUrl();

Step 7:Add the Token to your session.
We can authenticate to google with the code using the function authenticate().

if (isset($_GET['code'])) {
  $_SESSION['access_token'] = $client->getAccessToken();

Step 8: Set the Logout button.
Before getting into the authentication part we need to check whether the user has requested to clear the login information. That is, check whether the user reached the page by clicking the logout button.
We pass a logout parameter to the URL to indicate that the user has directed to logout. So we check for logout parameter in local URL. If the logout parameter is found then we clear the access token from the session.


Step 9: Set the access Token.
We check whether access token is set in the session. If yes, we can conclude that we have valid access token and so we can pass it to the client.

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

Step 10: Get Access Token
getAccessToken() method will return the access token if a valid access token is set to the client and otherwise it will return FALSE. If getAccessToken() returns FALSE, then we need to create the login URL with which the user can login. We use createAuthUrl() method for it.

  $plus = new Google_Service_Plus($client);
  $me = $plus->people->get('me');
  $optParams = array('maxResults' => 10);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);
Note: Download the file get the full code

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

3 Responses

  1. Bob says:

    The Download Link is not working. Can you fix it. Thanks.
    Regards, Bob

Leave a Reply

Your email address will not be published. Required fields are marked *