San*_*ngh 5 php google-api google-client google-api-php-client
我在多个页面上使用 Google API 时遇到问题。来自 Google 的示例工作正常,但所有代码都在一页上。
我有两页。第一页,用户点击登录按钮,第二页,我使用 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>
Run Code Online (Sandbox Code Playgroud)
第二页.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
Run Code Online (Sandbox Code Playgroud)
出于某种原因,secondPage.php 上的 if 语句导致 false 并且 else 语句将其重定向回 firstPage.php。
我对编程完全陌生,我很确定我正在做一些没有意义的事情。让我知道是否应该添加更多信息。在回答问题时,请尝试涵盖以下问题:
我对这条线确实有一些疑问:
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['secondPage.php'];
Run Code Online (Sandbox Code Playgroud)
这是因为我很确定,你没有在$_SERVER['secondPage.php']变量后面设置任何内容。您应该修复此问题以重定向到secondPage.php,然后首页中不再需要任何代码。
secondPage.php在您的第一个脚本中,您有这样的行:
$client = new Google_Client();
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);
Run Code Online (Sandbox Code Playgroud)
并且您已经将它们省略了secondPage.php。这可能就是您的第二个脚本不起作用的原因,因为您的脚本不知道它正在使用哪个帐户。您必须在第二个脚本中再次配置脚本,就像在第一个脚本中所做的那样。另外现在我会中断ob_start(),它可能会加强调试。
我希望您再次仔细阅读谷歌存储库上的示例。这是非常不言自明的,它只需要你一遍又一遍地阅读它,只要你会有那种奇怪的小感觉......我可以向你保证,你可以在三个文件中轻松干净地完成它:第一个文件是::authenticate()和set $_SESSION,第二个用于::setAccessToken()和所有其余的,第三个用于$client之前两者使用的所有类设置。