我正忙着尝试使用Google Calendar API将一个非常简单的事件添加到日历中,如果有人可以指出我(可能很明显)的问题,我会很高兴。我正在使用我发现here的代码。我已将代码放在“google-api-php-client / examples.calendar”目录中,其中可以找到一个简单的示例。
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('worked.html'); //I made a file called "worked.html" in the same directory that just says "it worked!"
$client->setDeveloperKey('SecretLongDeveloperKey');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$authUrl = $client->createAuthUrl();
if (!$client->getAccessToken()){
$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDateTime('2012-10-31T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-10-31T10:25:00.000-05:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('secretLongCalendarId@group.calendar.google.com', $event);
}
echo $createdEvent->getId();
?>
当我访问此脚本时,出现404错误。我试图通过代码并注释掉行以试图找到罪魁祸首 - 它似乎是倒数第二行,它实际上插入了事件。
有什么建议吗?我真的很感激一些指示,因为我似乎无法得到最简单的例子。
答案 0 :(得分:3)
您的代码几乎可以使用。
但是,您重定向到“working.html”。这样,您的事件在重定向身份验证后就会不创建。此外,setRedirectUri应与您在Google API plus控制台中输入的内容相匹配(请参阅“重定向URI”) AND 它应该是 THIS 文件,因为此文件在重定向后进入事件。 (您不需要“working.html”)
所以你的simple.php应该是这样的(也可以将Google Api上的“重定向URI”更改为http://localhost/simple.php
,你需要指定域但可以使用localhost,在setRedirectUri中你可以指定相同的)< / p>
<?php
error_reporting(E_ALL);
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
if ((isset($_SESSION)) && (!empty($_SESSION))) {
echo "There are cookies<br>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
}
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('###');
$client->setClientSecret('###');
$client->setRedirectUri('http://###/index.php');
$client->setDeveloperKey('###');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
echo "<br><br><font size=+2>Logging out</font>";
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
echo "<br>I got a code from Google = ".$_GET['code']; // You won't see this if redirected later
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
echo "<br>I got the token = ".$_SESSION['token']; // <-- not needed to get here unless location uncommented
}
if (isset($_SESSION['token'])) {
echo "<br>Getting access";
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()){
echo "<hr><font size=+1>I have access to your calendar</font>";
$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDateTime('2013-9-29T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-9-29T10:25:00.000-05:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('###', $event);
echo "<br><font size=+1>Event created</font>";
echo "<hr><br><font size=+1>Already connected</font> (No need to login)";
} else {
$authUrl = $client->createAuthUrl();
print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>";
}
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>";
?>
另外,就像@BigMacAttack已经说过的那样,你只需要这个
$authURL = $client->createAuthURL();
一次{,只有getAccessToken
失败。
万圣节快乐; - )
编辑:我通过登录和注销以及日志消息的工作链接清理了很多代码。
答案 1 :(得分:0)
代码的最后一部分没有使用正确的逻辑。问题的关键在于if (!$client->getAccessToken())
。在示例代码的上下文中,此语句大致转换为:如果您未能获取访问令牌,请创建事件。显然这不是你想要发生的事情。 ;)
而是试试这个:
if ($client->getAccessToken()){
//succeeded in getting an access token, so create and insert the event
} else {
$authUrl = $client->createAuthUrl();
//failed to get an access token, so display $authurl as a link to open the auth window
}