Skip to content

Instantly share code, notes, and snippets.

@insthync
Created July 8, 2018 12:09
Show Gist options
  • Save insthync/072695769a476b3b55819fd97a44294e to your computer and use it in GitHub Desktop.
Save insthync/072695769a476b3b55819fd97a44294e to your computer and use it in GitHub Desktop.
Google OAuth2 Callback
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/oauth2callback');
$client->addScope('https://www.googleapis.com/auth/plus.login');
$client->addScope('https://www.googleapis.com/auth/plus.me');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->addScope('https://www.googleapis.com/auth/userinfo.profile');
$client->setAccessType('online');
$client->setIncludeGrantedScopes(false);
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$credentials = $client->authenticate($_GET['code']);
if (isset($credentials['error']))
{
echo 'Error: ' . $credentials['error'] . ' Description: ' . $credentials['error_description'];
}
else
{
$_SESSION['access_token'] = $client->getAccessToken();
echo 'Success';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment