5

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.
Sandeep Singh
  • 897
  • 2
  • 8
  • 20

1 Answers1

0

1. Redirect properly

I do have some doubts about that line:

$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['secondPage.php'];

It's because I'm pretty sure, that you have nothing set behind $_SERVER['secondPage.php'] variable. You should fix this to redirect to secondPage.php, and afterwards no more code in first page is required.

2. Set up accont in secondPage.php

In your first script you have such lines:

$client = new Google_Client();
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);

And you have ommited them in secondPage.php. That's probably reason why your second script does not work, because your script does not know with what account it is working. You have to configure you script all again there in second script, same way you did in first one. Also for now I would cut-off ob_start(), it may harden debugging.

I prefer you to read that example on google repository once again, carefully. It's pretty self explanatory and it just requires you to read it again and again, as long as you will have that strange little feeling... I can assure you, that you can do it easy and clean in three files: first one for ::authenticate() and set $_SESSION, second for the ::setAccessToken() and all rest, and third one with all $client class set up used by previous both.

Community
  • 1
  • 1
yergo
  • 4,761
  • 2
  • 19
  • 41