在PHP中创建简单的Google日历事件 [英] Create Simple Google Calendar Event in PHP

查看:121
本文介绍了在PHP中创建简单的Google日历事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段时间试图使用Google Calendar API将日历添加到日历中,如果有人能指出我的(可能是显而易见的)问题,我会很喜欢它。我使用的代码是此处。我已将代码放在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'); //我在同一个目录中创建了一个名为worked.html的文件,它只是说它工作!
$ 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错误。我已经试过了代码和注释行,试图找到罪魁祸首 - 它似乎是倒数第二行,它实际上插入了事件。



有什么建议吗?我真的很感激一些指针,因为我甚至无法得到最简单的例子。

你的代码差不多works。



但是,您重定向到worked.html。这样,您的事件不是会在重定向身份验证后创建。此外,setRedirectUri应与您在Google API plus控制台中输入的内容相匹配(请参阅重定向URI) AND 它应该是这个文件,因为此文件在重定向后进入事件。 (您不需要worked.html)

因此,您的simple.php应该如下所示(也可以将Google Api上的重定向URI更改为 http://localhost/simple.php ,你需要指定域但可以使用localhost,在setRedirectUri中你可以指定相同的)

 <?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(); ((isset($ _ SESSION))&&(!empty($ _ SESSION))){
echoThere are cookies< br>;

if
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>注销< / font> ;
unset($ _ SESSION ['token']);
}

if(isset($ _ GET ['code'])){
echo< br>我从Google获得了一个代码=。$ _ GET ['码']; //如果稍后重定向,您将不会看到此内容
$ client-> authenticate($ _ GET ['code']);
$ _SESSION ['token'] = $ client-> getAccessToken();
header('Location:http://'。$ _SERVER ['HTTP_HOST']。$ _SERVER ['PHP_SELF']);
echo< br>我得到了令牌=。$ _ SESSION ['token']; //< - 不需要到这里,除非位置未注释
}

if(isset($ _ SESSION ['token'])){
echo< br> ;获取访问权限;
$ client-> setAccessToken($ _ SESSION ['token']);
}

if($ client-> getAccessToken()){

echo< hr>< font size = + 1>到您的日历< / 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>创建的事件< / font>;

echo< hr>< br>< font size = + 1>已连接< / font>(无需登录);

} 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>注销< / a>< / font>;

?>

另外,就像@BigMacAttack已经指出的那样,您只需要
$ authURL = $ client-> createAuthURL(); 一次,仅当 getAccessToken 失败。 b

万圣节快乐; - )

编辑:我通过工作链接登录并清理了很多代码注销和日志消息。


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日历事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆