Skip to content
Toggle menu
TrinityTuts TrinityTuts

Android, Php, Web Designing best tutorial

Primary Menu
  • Home
  • Useful Tips & Codes
  • Video Tutorials
  • Looking for help?
  • Home
  • google
  • Login with Google in your PHP web application

Login with Google in your PHP web application

Posted On : November 14, 2019 Published By : Aneh Thakur

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.

New Google project in Google console

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

Google Console create api for web application

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. 🙂

Share this:

  • LinkedIn
  • Pocket
  • Email
  • Telegram
  • Print
  • Twitter
  • Tumblr
  • Reddit
  • Pinterest
  • More
  • Facebook
Posted in: google, phpTagged : Google web sign in

Post navigation

Add Google reCaptcha in Android Application
Install and configure Redis cache in PHP web application




Follow Us On Facebook

Follow Us On Facebook

Usefull Tips

  • Run CakePHP 3 shell after specific interval using shell script
    Source: tips Published on 2019-10-09
  • Check time between two time PHP
    Source: tips Published on 2019-09-07
  • Cancel current or queue request in OKHTTP3
    Source: tips Published on 2019-09-04

Quick Links

  • Useful Tips & Codes
  • Youtube Channel
  • Privacy Policy
  • Help
  • Wholesale Sarees Catalogs

ABOUT

Trinity tuts is one of the best place for beginners to learn android, php, google and web design tutorial and tips. - Maintained by Aneh Thakur

Subscribe to Blog via Email

Join 4,645 other subscribers

Copyright All right reserved
TrinityTuts TrinityTuts

Android, Php, Web Designing best tutorial

Theme: Infinity Mag by ThemeinWP
scroll to top
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.