Skip to content

Instantly share code, notes, and snippets.

@arodu
Created April 11, 2019 15:45
Show Gist options
  • Select an option

  • Save arodu/a14590b8cdc609afd116a8160ff615ed to your computer and use it in GitHub Desktop.

Select an option

Save arodu/a14590b8cdc609afd116a8160ff615ed to your computer and use it in GitHub Desktop.
<?php
require_once('settings.php');
require_once('google-login-api.php');
// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
try {
$gapi = new GoogleLoginApi();
// Get the access token
$data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
// Get user information
$user_info = $gapi->GetUserProfileInfo($data['access_token']);
}
catch(Exception $e) {
echo $e->getMessage();
exit();
}
}
?>
<head>
<style type="text/css">
#information-container {
width: 400px;
margin: 50px auto;
padding: 20px;
border: 1px solid #cccccc;
}
.information {
margin: 0 0 30px 0;
}
.information label {
display: inline-block;
vertical-align: middle;
width: 150px;
font-weight: 700;
}
.information span {
display: inline-block;
vertical-align: middle;
}
.information img {
display: inline-block;
vertical-align: middle;
width: 100px;
}
</style>
</head>
<body>
<div id="information-container">
<div class="information">
<label>Name</label><span><?= $user_info['name'] ?></span>
</div>
<div class="information">
<label>ID</label><span><?= $user_info['id'] ?></span>
</div>
<div class="information">
<label>Email</label><span><?= $user_info['email'] ?></span>
</div>
<div class="information">
<label>Email Verified</label><span><?= $user_info['verified_email'] == true ? 'Yes' : 'No' ?></span>
</div>
<div class="information">
<label>Picture</label><img src="<?= $user_info['picture'] ?>" />
</div>
</div>
</body>
</html>
<?php
class GoogleLoginApi{
public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code){
$url = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to receieve access token');
return $data;
}
public function GetUserProfileInfo($access_token){
$url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to get user information');
return $data;
}
}
?>
<?php require_once('settings.php'); ?>
<html>
<head>
<style type="text/css">
#login-button {
display: block;
text-align: center;
margin: 50px 0;
}
</style>
</head>
<body>
<a id="login-button" href="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">Login with Google</a>
</body>
</html>
1) Go to Google API Console : https://console.developers.google.com/
2) If you have not created a project, create a project by clicking Create Project (at the top)
3) After the project is created, select the created project from the top dropdown.
4) Now click on "Credentials" tab on the left sidebar. In the next screen click on "OAuth consent screen".
Fill out Application Name and Authorized domains with the domain from where you intend to run the application.
If you are just testing it out on localhost, you can Authorized domains as blank.
5) Click on the button Create Credentials. Choose OAuth client ID in the dropdown.
6) In the next page choose application type as Web application. Add a name.
7) Add a redirect url in the section Authorised redirect URIs. This url should point to the redirect url script. (gauth.php in the attached codes). You can add a localhost url if you want.
8) You can leave out Authorised JavaScript origins as blank. Click on the Create button.
9) On success you will get the App Client ID and App Secret. Save those as they will be required later.
10) Edit settings.php and add your Client ID, Client Secret and Redirect Url
11) Run index.php in the browser
For more information visit the tutorial : http://usefulangle.com/post/9/google-login-api-with-php-curl
<?php
/* Google App Client Id */
define('CLIENT_ID', 'XXXXXXXXXXXXXXXXXXXXXXX');
/* Google App Client Secret */
define('CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXX');
/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'XXXXXXXXXXXXXXXXXXXXXXX');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment