Google APIs Client Library

The Google API Client Library enables you to work with Google APIs such as Gmail, Drive or YouTube on your serve.

Installation

Composer

You can use Composer or simply Download the Release. Follow the installation instructions if you do not already have composer installed.

Once composer is installed, execute the following command in your project root to install this library:

composer require google/apiclient

Finally, be sure to include the autoloader at login_google.php:

require_once 'vendor/autoload.php';

\

Google Cloud Console Setup

Open Google Cloud Console, Select the [button] on the left upper corner and create a new project.

Select [APIs and services]

Select [OAuth consent screen], select [External] as User Type, and [Create]

The label which has * is compulsary to fill in, etc [App name], [User support email] and [Email address]. Other is optional and can left empty.

You can leave it blank at this [Scope] page

You can leave it blank at this [Test user] page.

Check the data fill in, if nothing wrong, then [BACK TO DASHBOARRD]

Remember to set the publishing status to publish by clicking [PUBLISH APP]. Noted: If you were not set to publish, then you have to add your test user email account to able use the google API

Select Credentials, then [CREATE CREDENTIALS] >> [OAuth client ID]

Select [Application type] as Web application, fill in the [Name] and [Authorised redirect URLs] of your login page. In my case, i build my login page at http://localhost:3000/login_google.php.

Copy the [client ID] and [Client Secret] here. It will be using later on.

Implementation on Your Website

login_google.php

replace the $clientID and $clientSecret to your [client ID] and [Client Secret]

<?php
// Include config file
require_once 'vendor/autoload.php';


// Google Login Start  ------------------------------------------------------------------------------------------------------------------------

// Initialize Google API client
$clientID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$redirectUrl = 'http://localhost:3000/login_google.php';
$client = new Google\Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUrl);
$client->addScope('profile');
$client->addScope('email');

//Create a URL to obtain user authorization
// If user is not authenticated, redirect to Google sign-in page
if (!isset($_GET['code'])) {
    $auth_url = $client->createAuthUrl();
    header("Location:  $auth_url");
} else {
    //get user info from google account
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $client->setAccessToken($token);
    $service = new Google\Service\Oauth2($client);
    $user = $service->userinfo->get();
    $email = $user->email;
    $first_name = $user->givenName;
    $last_name = $user->familyName;
    $name =  $user->name;
    $gender = $user->gender;
    $profile_picture = $user->picture;
    $id = $user->id; //used
    $hd = $user->hd;
    $password =  password_hash($id, PASSWORD_DEFAULT); // take google id as password(after encrypted)
    $username = "$first_name, $last_name"; //the reason use format [$first name, $last name] instead of $name, is to avoid repeated username in users table


    // your action here
    echo $username;

}

button.php

Create a login button to header to login.php

PHP

    // if google login
    if (isset($_POST['googleLogin'])) {
        header("location: login.php");
    }

HTML

<button name="googleLogin" class="login-with-google-btn">
  Sign in with Google
</button>

Last updated