5

Google カレンダー 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'); //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 エラーが発生します。犯人を見つけるために、コードを調べて行をコメントアウトしようとしました-実際にイベントを挿入するのは最後から2番目の行のようです。

何かアドバイス?最も単純な例でさえ機能させることができないように見えるので、いくつかの指針を本当に感謝します。

4

2 に答える 2

4

あなたのコードはほとんど機能します。

ただし、「worked.html」にリダイレクトします。そうすれば、認証のリダイレクト後にイベントが作成されません。また、setRedirectUri は、Google API とコンソールで入力したものと一致する必要があります (「リダイレクト URI」を参照) 。このファイルは、リダイレクト後にイベントに入るため、このファイルである必要があります。(「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->createAuthURL();場合にのみ必要です。getAccessToken

ハッピーハロウィン;-)

編集:ログインとログアウト、およびログメッセージへのリンクを使用して、コードを大幅にクリーンアップしました。

于 2013-09-13T11:51:44.447 に答える
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
}
于 2013-09-11T05:46:56.710 に答える