Login with Google using PHP and Google PHP Client

In this article we’ll learn how to implement Google login into your web application using Google PHP Client version 2. It’s assumed that you have already installed composer and are using a version of PHP greater than 5.4. To view the entire code please visit this gist.

Create OAuth 2.0 Client ID and Secret Key

To get started we’ll need Client ID and Client Secret. For that we’ll have to create a Google API console project. Follow belows steps.

  • Go to Google API Console
  • Click on the Projects drop down in top left corner.
    Click on project tab
  • In the popup that shows up click on “New Project” in top right.
    Select new project
  • Click on “Create”.
    Create Project Modal
  • Click on “Credentials” in left sidebar.
    Click on Credentials
  • Click on “OAuth consent screen”.
    OAuth Screen
  • In “Application name” specify any name of your choice
  • “Support email” must have autofilled your current Gmail address. Feel free to change it to any different email address.
  • In “Authorized domains” specify the domain that you would be using. If you are testing this locally you can enter lvh.me which is same as localhost.
  • You can leave rest other fields empty.
  • Click on save
  • Then in “Credentials” tab click on “Create credentials” and then “OAuth client ID”.
  • Select “Web application”.
    Redirect urls
  • Enter any name in “Name”.
  • Whatever url is specified in “Authorized redirect URIs” post login Google would be redirecting the user to this url with some authentication data. So if you are trying this out locally you can specify http://lvh.me/login.php. We would be creating login.php script shortly. Feel free to change the url as per your domain.
  • Click on “Create”
  • Note down the Client ID and Client Secret in the modal that shows up. We’ll be needing this in our login script.

Google client library

Google has provided PHP library that has lots of functions to interact with Google APIs usign OAuth2. Let’s go ahead and install it. Assuming your project root is /var/www/html issue below commands.

Create login script

Create a login.php file. Include vendor/autoload.php so that we can include the Google library we installed using Composer.

Now let’s define some constants for Google Client ID and Google Client Secret.

GOOGLE_REDIRECT_URL is url to current login.php script. Feel free to change it as per your domain.

Now let’s create a new instance of Google client.

Post login Google would be redirecting user with code query string. We’ll be needing this to generate OAuth2 token.

Whenever we have code in query string we fetch the token and set a Session. If Session is set then we’ll assume user is logged in. If not then we’ll show him a login url.

Please note that we haven’t handled all error cases. Like say in current script if we receive any invalid value in code query string then that case needs to be handled.

To download the entire code please visit this gist.

Leave a Reply

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