How to work with Google api on multiple pages?

I'm having trouble working with the Google APIs on multiple pages. Example from Google is working fine, but all of the code is on the same page.

I have two pages. The first page is where the user clicks the login button and the second page is where I use google api to retrieve the 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 the secondPage.php statement results in false, and the else statement redirects it back to firstPage.php.

I have a lot of new things in programming, I am sure that I am doing something that does not make sense. Let me know if I need to add more information. When answering a question, please try to answer the following questions:

  • Google_client .
  • Google_client, access_token .
  • , firstPage.php , access_token google.
  • , googleapi, firstPage.php secondPage.php.
+4
1

1.

:

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

, , $_SERVER['secondPage.php']. , secondPage.php, .

2. accont secondPage.php

script :

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

secondPage.php. , , script , script , . script script, . ob_start(), .

, Google, . , , , ... , : ::authenticate() $_SESSION, ::setAccessToken() , - $client, .

0

Source: https://habr.com/ru/post/1584853/


All Articles