I am having trouble working with Google API on multiple pages. Example from Google works fine but all the code is on one page.
I have two pages. First page, where user click on login button and second page, where I use google api to pull user information.
First Page:
<?php
########## Google Settings.. Client ID, Client Secret from https://cloud.google.com/console #############
$google_client_id = 'myid';
$google_client_secret = 'mysecret';
$google_redirect_url = 'http://www.myWebsite.com/secondPage.php'; //path to your script
$google_developer_key = 'mydeveloperkey';
//include google api files
require_once '../includes/Google/autoload.php';
//start session
session_start();
$client = new Google_Client();
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->addScope("https://www.googleapis.com/auth/calendar");
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
$client->addScope("https://www.googleapis.com/auth/plus.me");
$drive_service = new Google_Service_Drive($client);
$calendar_service = new Google_Service_Calendar($client);
$gmail_service = new Google_Service_Gmail($client);
/************************************************
If we're logging out we just need to clear our
local access token in this case
************************************************/
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
// Authenticating the aunthentication URL. and starting session
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['secondPage.php'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
/************************************************
If we have an access token, we can make redirect to secondPage.php, else we generate an authentication URL.
************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
header("Location: http://www.myWebsite.com/secondPage.php");
die();
} else {
$authUrl = $client->createAuthUrl();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<a href="<?php echo $authUrl; ?>"> <img src="images/google-login-button.png" alt="Click to login"></a>
</body>
</html>
secondPage.php:
<?php ob_start() ?>
<?php
//include google api files
require_once '../includes/Google/autoload.php';
//start session
session_start();
$client = new Google_Client();
/************************************************
If we have an access token, we can make
requests, else we redirect to firstPage.php.
************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
header("Location: http://www.myWebsite.com/firstPage.php");
die();
}
// Rest is HTML
For some reason if statement on secondPage.php is resulting into false and else statement is redirecting it back to firstPage.php.
I am very new to programming altogether, I am pretty sure I am doing something which does not make sense. Let me know if I should add more information. When answering question please try to cover following questions:
- Do I have to create separate Google_client object on every page.
- Can I create Google_client object by setting access_token from session variable.
- How should I divide the code so that on the firstPage.php, only have a login button and all other pages can use access_token to use google services.
- there will be other pages where I will be using googleapi other than firstPage.php and secondPage.php.