我花了很长时间尝试使用 Google 日历 API 将一个非常简单的事件添加到日历中,如果有人能指出我的(可能很明显的)问题,我会很高兴的.我正在使用在此处找到的代码.我已经把代码放在了google-api-php-client/examples.calendar"目录下,在那里可以找到一个简单的例子.
setApplicationName("Google Calendar PHP Starter Application");$client->setClientId('');$client->setClientSecret('');$client->setRedirectUri('worked.html');//我在同一个目录中创建了一个名为worked.html"的文件,上面写着它工作了!"$client->setDeveloperKey('SecretLongDeveloperKey');$cal = new Google_CalendarService($client);如果(isset($_GET['注销'])){未设置($_SESSION['token']);}如果(isset($_GET['代码'])){$client->authenticate($_GET['code']);$_SESSION['token'] = $client->getAccessToken();header('位置:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);}如果 (isset($_SESSION['token'])) {$client->setAccessToken($_SESSION['token']);}$authUrl = $client->createAuthUrl();如果 (!$client->getAccessToken()){$event = new Google_Event();$event->setSummary('万圣节');$event->setLocation('邻里');$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 错误.我已经尝试浏览代码并注释掉行以试图找到罪魁祸首 - 它似乎是倒数第二行,实际上插入了事件.
有什么建议吗?我真的很感激一些建议,因为我似乎连最简单的例子都无法工作.
你的代码几乎可以工作了.
但是,您重定向到worked.html".这样,您的事件在身份验证重定向后不会被创建.此外,setRedirectUri 应与您在 Google API 和控制台中输入的内容相匹配(请参阅重定向 URI")AND它应该是 THIS 文件,因为此文件在重定向后进入事件.(你不需要worked.html")
所以你的 simple.php 应该看起来像这样(同时将 Google Api 上的重定向 URI"更改为 http://localhost/simple.php
,你需要指定域但可以使用localhost,在setRedirectUri中可以指定相同)
";echo "";打印_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);如果(isset($_GET['注销'])){echo "<br><br><font size=+2>注销</font>";未设置($_SESSION['token']);}如果(isset($_GET['代码'])){echo "<br>我从 Google 得到了一个代码 = ".$_GET['code'];//如果稍后重定向,您将看不到此内容$client->authenticate($_GET['code']);$_SESSION['token'] = $client->getAccessToken();header('位置:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);echo "<br>我得到了令牌 = ".$_SESSION['token'];//<-- 除非位置未注释,否则不需要到达这里}如果 (isset($_SESSION['token'])) {echo "<br>获取访问权限";$client->setAccessToken($_SESSION['token']);}如果 ($client->getAccessToken()){echo "<hr><font size=+1>我可以访问你的日历</font>";$event = new Google_Event();$event->setSummary('万圣节');$event->setLocation('邻里');$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>事件创建</font>";echo "<hr><br><font size=+1>已连接</font>(无需登录)";} 别的 {$authUrl = $client->createAuthUrl();print "<hr><br><font size=+2><a href='$authUrl'>联系我!</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
失败时.
万圣节快乐;-)
编辑:我使用登录和注销以及日志消息的工作链接对代码进行了大量清理.
I'm having a heck of a time trying to get a very simple event added to a calendar using the Google Calendar API, and I would love it if someone could point out my (probably obvious) issue. I'm using code that I found here. I've put the code in the "google-api-php-client/examples.calendar" directory, where a simple example can be found.
<?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();
?>
When I access this script, I get a 404 error. I've tried going through the code and commenting out lines in an attempt to find the culprit - it appears to be the second-to-last line, which actually inserts the event.
Any advice? I'd really appreciate some pointers, as I cannot seem to get even the simplest of examples to work.
Your code almost works.
However, you redirect to "worked.html". That way your event does not get created after the redirect of authentication. Also the setRedirectUri should match what you entered in Google API plus console (see "Redirect URIs") AND it should be THIS file because this file is entering the event after the redirect. (You don't need the "worked.html")
So your simple.php should look like this (ALSO change the "Redirect URIs" on Google Api to http://localhost/simple.php
, you need to specify the domain but can use localhost, in setRedirectUri you can specify the same)
<?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>";
?>
Also, like @BigMacAttack already stated, you only need the
$authURL = $client->createAuthURL();
once, only if getAccessToken
failed.
Happy Halloween ;-)
Edit: I cleaned up the code a lot with working links to login and logout and log-messages.
这篇关于用 PHP 创建简单的 Google 日历事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!