TrinityTuts

SignIn with Google in PHP web application

Last updated on August 28th, 2020 at 04:46 pm

In this post, I will explain to you how we can integrate Google Sign in our web application. Before we start integrating Google Sign In in our application we need to create a new project inside Google Console. You can follow below simple steps to create a project in the Google Console and get the required API key for your application.

Step 1. Go to Google Console and login to your Google Account.
Step 2. Create a new project in the console as shown in the below image.

Step 3. Now go to APIs & service menu click the OAuth consent screen and add the required information.

Step 4. Now we need to create credentials go to the left menu bar and click on the credentials menu as and select OAuth Client ID as shown in the below image.

Step 5. Now in this step, you need to add some more information like callback URL for your application as shown in the below screenshot.

Up to this step, we have our Google console project is ready and we have our keys. Now we need to add Google APIs Client Library for PHP in our application.

Setup Google API Client in PHP project

Now in this section, I will explain to you how we can add and configure Google Client in our PHP project. You can add the Google library in your PHP project using the composer. You can add a composer from their official website:- https://getcomposer.org/

Once you install the composer you can open the terminal and run below command inside your project.

composer require google/apiclient:"^2.0"

Now include this library in your project and add credentials and callback URL and secret to create login URL as shown in the below code.

<?php
require_once 'vendor/autoload.php';
$clientID = 'MYID-LIKE-THIS-njdd2e50rri5217.apps.googleusercontent.com';
$clientSecret = 'YOUR_SECRET';
$redirectUri = 'https://trinitytuts.com/g-auth';
$client = new \Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->addScope("email");
$client->addScope("profile");
$googleUrl = $client->createAuthUrl();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Google Login</title>
</head>

<body>
    <a href="<?=$googleUrl?>">Login with Google</a>
</body>
</html>

Now create one more file which handles the callback from Google server which has login data token in the result as shown in the below code.

<?php
require_once 'vendor/autoload.php';
$clientID = 'MYID-LIKE-THIS-njdd2e50rri5217.apps.googleusercontent.com';
$clientSecret = 'YOUR_SECRET';
$redirectUri = 'https://trinitytuts.com/g-auth';
$client = new \Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->addScope("email");
$client->addScope("profile");
if (isset($_GET['code'])) {
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $client->setAccessToken($token['access_token']);

    $google_oauth = new \Google_Service_Oauth2($client);
    $google_account_info = $google_oauth->userinfo->get();

    $email = $google_account_info->email;
    $id = $google_account_info->id;
    $name = $google_account_info->name;
}
?>

That’s it. I hope this post helps you in the integration of Google API client in your PHP web application. 🙂

You can also Read:- SignIn with Google in your Android Application