📄 Source: oauth2callback.php
<?php
session_start();
require_once 'vendor/autoload.php';
// Hardcode the URL
$baseUrl = "http://createcraftweb.playit.plus/AgentAI";
$client = new Google\Client();
$client->setClientId('1082083393389-c5uekspcp2boc1gd0n0gr3v1a5vbb3jg.apps.googleusercontent.com');
$client->setClientSecret('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');
$client->setRedirectUri("$baseUrl/oauth2callback.php");
$client->addScope('https://www.googleapis.com/auth/gmail.readonly');
$client->setAccessType('offline');
$client->setPrompt('consent');
if (isset($_GET['code'])) {
try {
// Fetch the token using the authorization code
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
if (!isset($token['error'])) {
$_SESSION['access_token'] = $token;
// Store refresh token separately for persistent login
if (isset($token['refresh_token'])) {
$_SESSION['refresh_token'] = $token['refresh_token'];
}
// Create tmp folder if it doesn't exist
if (!is_dir(__DIR__ . '/tmp')) {
mkdir(__DIR__ . '/tmp', 0755, true);
}
file_put_contents(__DIR__ . '/tmp/token.json', json_encode($token));
// Set cookie for persistent login (30 days)
setcookie('inboxzero_logged_in', 'true', time() + (86400 * 30), '/');
// Try to get user email for personalization
try {
$oauth2 = new Google\Service\Oauth2($client);
$userInfo = $oauth2->userinfo->get();
$_SESSION['user_email'] = $userInfo->email;
} catch (Exception $e) {
// Non-critical error
}
header('Location: index.php');
exit();
} else {
throw new Exception($token['error_description'] ?? 'Unknown error');
}
} catch (Exception $e) {
echo "Authentication failed: " . $e->getMessage();
echo "<br><a href='index.php'>Try again</a>";
}
} else {
echo "No authorization code received.";
echo "<br><a href='index.php'>Go back</a>";
}
?>
← Back