【问题标题】:Create Simple Google Calendar Event in PHP用 PHP 创建简单的 Google 日历事件
【发布时间】:2013-09-09 02:13:48
【问题描述】:

我正在尝试使用 Google 日历 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 错误。我尝试通过代码并注释掉行以试图找到罪魁祸首 - 它似乎是倒数第二行,实际上插入了事件。

有什么建议吗?我真的很感激一些指点,因为我似乎连最简单的例子都无法工作。

【问题讨论】:

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


    【解决方案1】:

    您的代码几乎可以运行。

    但是,您重定向到“worked.html”。这样,您的事件不会在身份验证重定向后创建。此外,setRedirectUri 应该与您在 Google API 和控制台中输入的内容相匹配(请参阅“重定向 URI”)AND 它应该是 THIS 文件,因为此文件在重定向后进入事件. (你不需要“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();
    
    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-&gt;createAuthURL(); 一次,只有当getAccessToken 失败时。

    万圣节快乐 ;-)

    编辑:我用登录和注销的工作链接以及日志消息清理了很多代码。

    【讨论】:

    • 这似乎非常接近,但还没有完全实现。这是我当前的代码修订版:pastebin.com/QJ7WTQfq 我为此创建了一个子域 test.mywebsite.com。所有代码都在 index.php 中。我用你的代码替换了我的代码,为所有### 输入正确的值,然后将 test.mywebsite.com/index.php 作为重定向 uri。转到页面,我进入“其他”部分,它给了我一个 URL。我去那个 URL,谷歌要求我授权,我点击接受,它把我发回 index.php,然后我进入“得到代码”部分。它从来没有真正进入插入。
    • 我又拿了你的代码,在这里它可以工作了。也许您的 cookie 有问题。您可以尝试在session_start() 之后使用print_r($_SESSION); 进行调试。您应该看到 cookie token 等。也许您也可以注释掉 header('Location: 行。您不需要重定向到同一个文件。您可以查看您是否到达“获取令牌”和“获取令牌”行。我不知道 Google 代码本身是否需要 cookie 才能工作。
    • 首先让我非常感谢您的帮助!你是我能找到的唯一资源! =) 我无法让它在另一个浏览器或另一台计算机上工作。我试着在你说的地方放一个 print_r,它只打印 Array(),我相信 (?) 意味着数组中没有任何内容 - 是真的吗?还有什么我可以 print_r() 来帮助诊断问题的吗?
    • 如果你只得到Array(),这意味着$_SESSION是空的,所以没有cookies通过。您正在“获取代码”,因此应该在重定向到自身之前设置 cookie 'token' ($_SESSION['token'])。之后它应该在 $_SESSION.您是否尝试删除 header('Location: 行?您也可以尝试在$_SESSION['token'] = $client-&gt;getAccessToken(); 之后立即执行print("i got the token = ".$_SESSION['token']); exit(); 以查看令牌是否已填充。 (exit(); 很重要,否则你会被重定向。
    • 在执行header('Location: 之前输出一行“获取代码”的方式也不起作用。仅当您尚未输出任何内容时,标头才有效。所以你不会被重定向。 (我会尝试注释掉header('Location:-line。它在这里工作。)
    【解决方案2】:

    您的代码的最后一部分没有使用正确的逻辑。问题的症结在于if (!$client-&gt;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
    }
    

    【讨论】:

    • 嗯,这似乎有效,但它并不是我想要的,或者它可能只是无法正常工作。我想要的是能够运行 php 脚本,然后将一个事件添加到我为此目的设置的日历中。似乎正在发生的事情是,每次我运行脚本时,都会询问我是否授权应用程序能够编辑我的日历。当我单击“确定”时,我会被发送到我的重定向 URI,但实际上并没有将任何事件添加到日历中。再次运行脚本只会让我重复授权过程。这是我当前的代码:pastebin.com/KTBuh6B6
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    相关资源
    最近更新 更多