Google Authentication API


English version steps

  1. First click on the video link below and follow this video to configure what you need on Google Cloud Console. You can stop watching at 5:31. Video link: https://www.youtube.com/watch?v=xH6hAW3EqLk

  1. PHP implementation requires downloading a google-api-php-client. click on this link below to download. Where my version is PHP 7.4, you can also choose other versions. Link: https://github.com/googleapis/google-api-php-client/releases

  1. After downloading and unzipping, put the whole folder into your project root directory. Your project needs a folder called google-api-php-client–PHP7.4 (7.4 is the version)

  1. Then, you can start doing it. You can follow this PHP code at the code section at the end of this article to implement an example of a user logging in with a Google account and adjust it yourself to fit your needs.

中文步骤

  1. 首先点击以下视频链接,跟着这个视频在Google Cloud Console上配置好所需的东西。你可以在第5:31停止观看。视频链接:https://www.youtube.com/watch?v=xH6hAW3EqLk

  1. PHP实现需要下载一个google-api-php-client。点击以下这个链接进行下载。其中我的版本是PHP7.4,你也可以选择其它版本。链接:https://github.com/googleapis/google-api-php-client/releases

  1. 下载完解压缩后,把整个文件夹放到你的项目根目录里。你的项目里需要有个文件夹叫google-api-php-client–PHP7.4 (7.4为版本)

  1. 然后,就可以开始做了。可以根据在文章最后的Code部分的PHP代码实现用户用Google账户登录的例子,自行调整以符合自己的需求。

Code Section (PHP代码部分)

<?php
// You can put this code in a file and use require_once "this file name.php"; at the start of your login.php
session_start(); // you can delete it based on your need
require_once 'google-api-php-client--PHP7.4/vendor/autoload.php'; // invoke the php client you downloaded at the step 2 and 3 (make sure the whole php client folder is at the root of the project in this case)
$loginButton = ''; // you can ignore this
$gClient =  new Google\Client(); // initialize
$gClient->setClientId('Your Client ID'); // This must be the same as the one in your google cloud console
$gClient->setClientSecret('Your Client Secret'); // This must be the same as the one in your google cloud console
$gClient->setApplicationName("Your app name");// This is also from your google console and must be the same as the one in your google cloud console
$gClient->setRedirectUri('http://assignmentmanagerxxxxx.xxxxxxx.xxxxxx.elasticbeanstalk.com/login.php'); // the redirect url must be the same as your google cloud console! AND you may not set up an redirect url from step 1 so you need to go to your console and set up ONLY 1 redirect URL and that URL must MATCH to this one in the code here. (redirect URl is where/which page would you like to go after the user login with their google account. in this case i redirect the user to my login page. you may adjust this to match your need)
$gClient->setAccessType('offline');
$gClient->setApprovalPrompt('force');
$gClient->addScope('profile');
$gClient->addScope('email');

if (isset($_GET['code'])) { //receive user information, if is set means the user is login with google. (after the user login with their google account, since this file is at the top of the login page and the redirect url is going to redirect to the login page after the user login with their google account, so it will detect the user is logged in with their google account)
	$token = $gClient->fetchAccessTokenWithAuthCode($_GET['code']);
	$_SESSION["access_token"] = $token; // if you doing session make sure to unset all when you do logout. You also need to do "$gClient->revokeToken();" in your logout page to make it secure
	$gClient->setAccessToken($token); //this may give http 500 error because the client id and secret key is not match with your google cloud console!
	$oAuth = new Google\Service\Oauth2($gClient);
	$userData = $oAuth->userinfo->get(); // get user data
    // below just put their name id email to the session.
	$_SESSION["currentUserId"] = $userData->id;
	$_SESSION["currentUserEmail"] = $userData->email;
	$_SESSION["user_name"] = $userData->givenName;

    // you can adjust it or delete it based on your need
	if (isset($_SESSION["currentUserId"])) { // if id set means user logged in successfully, then redirect them to the page
		header('Location:manager.php');
		exit();
	}
} else { // Here is also important!
    // when the user click login with google button, it should create a auth url and let the user to choose a google account ot login.
    // you can adjust it but this $gClient->createAuthUrl() is crucial.
	$loginButton = '<a class="googleLogin" href="' . $gClient->createAuthUrl() . '">Login with Google</a>';
}
?>

Author: Anti-General
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Anti-General !
R
Comment
  TOC