0

Google APIコンソールhttps://code.google.com/apis/consoleをアクティブ化し、OAuthクレデンシャルを含むプロジェクトを作成しました。「デスクトップアプリケーション」設定を選択しました。これは、このアプリがブラウザーの外部で、CLIから(実際には私のコンピューター上でのみ)実行されるためです。

https://code.google.com/p/google-api-php-client/で説明されているように、クライアントライブラリもダウンロードしました。

google-api-php-client/src/config.phpキー、、、、およびその他のapplication_name編集をoauth2_client_id編集しoauth2_client_secretました。oauth2_redirect_urideveloper_key

他のすべてのサービスも削除しservicesました。

これで、ガイドのサンプルからまとめた次のアプリができました。

<?php
require_once 'google-api-php-client/src/apiClient.php';
require_once "google-api-php-client/src/contrib/apiCalendarService.php";

session_start();

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);


if (isset($_SESSION['oauth_access_token'])) {
  $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
  $token = $apiClient->authenticate();
  $_SESSION['oauth_access_token'] = $token;
}

$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20110701T100000-07:00'));
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
                   // ...
                   );
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('primary', $event);

echo $recurringEvent->getId();

ただし、次のエラーが発生します。

Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required' in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86

apiServiceException: Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86

Call Stack:
    0.0003     252600   1. {main}() /home/flav/projects/.../gcal-api.php:0
    0.0202    1769344   2. EventsServiceResource->insert() /home/flav/projects/.../gcal-api.php:38
    0.0202    1770288   3. apiServiceResource->__call() /home/flav/projects/.../google-api-php-client/src/contrib/apiCalendarService.php:493
    0.0206    1781864   4. apiREST::execute() /home/flav/projects/.../google-api-php-client/src/service/apiServiceResource.php:187
    0.2342    1794848   5. apiREST::decodeHttpResponse() /home/flav/projects/.../google-api-php-client/src/io/apiREST.php:56

これを修正する方法は?

ああ、そしてグーグルAPIコンソールで、私はとの2つの値を持っています、Redirect URIs:どちらに使うべきですか?urn:ietf:wg:oauth:2.0:oobhttp://localhostoauth2_redirect_uri

4

1 に答える 1

0

これはいくつかの原因が考えられます。私の2セント:

複数のカレンダーを登録していますか?イベントを挿入したいカレンダーが「プライマリ」(=デフォルト?)カレンダーではない可能性があります(またはOauth credデータが別のカレンダーを参照しています)。その場合は、この行を置き換えます

$service->events->insert('primary', $event);

$calendar_id = "somelongstring@group.calendar.google.com"; //look this up from calendar /settings calendar details settings page at the bottom
$service->events->insert($calendar_id, $event);

カレンダーIDを見つけるのは少し難しいです...それを調べてください。たとえば、Googleアカウントのホームページ/カレンダー/歯車の記号/設定/クリックしたカレンダー名/カレンダーの詳細設定ページの下部にあります。

私のリダイレクトURL(ただし、Webアプリの場合)は、oauthリクエストを発行した.phpページを指しています。認証された場合は、別のリダイレクトを定義します。今回は自分でリダイレクトします。紛らわしい?はい、でもそれは私にとってはうまくいきます。

于 2012-09-11T13:56:55.003 に答える