Skip to content
TrinityTuts TrinityTuts

Android, Php, Web Designing best tutorial

Primary Menu
  • Home
  • Useful Tips & Codes
  • Homemade Recipe
  • Video Tutorials
  • Looking for help?
  • Youtube Thumbnail Extractor
  • Home
  • google
  • SignIn with Google in PHP web application

SignIn with Google in PHP web application

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

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.

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

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

Share this:

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

Post navigation

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

Advertisement




Follow Us On Facebook

Follow Us On Facebook

Usefull Tips

  • Fixing 413 request entity too large PHP NGINX server
    Source: tips Published on 2020-11-26
  • Steps to Install NodeJs LTS on Ubuntu OS
    Source: tips Published on 2020-11-19
  • Get Android Advertisement ID (AAID) programmatically
    Source: tips Published on 2020-10-19

Advertisement —-




Quick Links

  • Useful Tips & Codes
  • Youtube Channel
  • Privacy Policy
  • Recipes, Dinners and Easy Meal Ideas
  • Help
  • Wholesale sarees online

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,795 other subscribers

  • Wholesale sarees online
Copyright All right reserved
TrinityTuts TrinityTuts

Android, Php, Web Designing best tutorial

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