如何在多个页面上使用Google API?

时间:2015-04-27 08:48:32

标签: php google-api google-api-php-client google-client

我在多个网页上使用Google API时遇到问题。来自Google的Example工作正常,但所有代码都在一页上。

我有两页。第一页,用户点击登录按钮和第二页,我使用google api来提取用户信息。

第一页:

<?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

由于某种原因,如果secondPage.php上的语句导致为false,则else语句将其重定向回firstPage.php。

我对编程非常陌生,我很确定我正在做一些没有意义的事情。如果我应该添加更多信息,请告诉我。在回答问题时,请尝试涵盖以下问题:

  • 我是否必须在每个页面上创建单独的Google_client对象。
  • 我是否可以通过从会话变量设置access_token来创建Google_client对象。
  • 我应该如何划分代码,以便在firstPage.php上只有一个登录按钮,所有其他页面都可以使用access_token来使用google服务。
  • 除了firstPage.php和secondPage.php之外,还有其他我将使用googleapi的页面。

1 个答案:

答案 0 :(得分:0)

1。正确重定向

我对这一行有一些疑问:

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

这是因为我很确定你在$_SERVER['secondPage.php']变量后面没有任何设置。您应该修复此问题以重定向到secondPage.php,然后在第一页中不再需要代码。

2。在secondPage.php

中设置accont

在你的第一个脚本中你有这样的行:

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

你已经在secondPage.php中提出了这些问题。这可能是您的第二个脚本不起作用的原因,因为您的脚本不知道它正在使用什么帐户。您必须在第二个脚本中再次配置脚本,就像在第一个脚本中一样。现在我也会切断ob_start(),它可能会加强调试。

我希望您再次仔细阅读google存储库中的该示例。这是非常自我解释的,它只需要你一次又一次地阅读它,只要你会有那种奇怪的小感觉......我可以向你保证,你可以在三个文件中轻松完成:第一个用于{ {1}}并设置::authenticate(),其次为$_SESSION,其余为::setAccessToken(),第三个设置为前两者使用的所有$client类。