2

基本的な要約:シンジケーションと印刷を容易にするために、人々が予定をスケジュールしてタイムスロットを予約できるようにするクライアント側のカレンダーを作成しました (たとえば、予約はデータベースに記録され、2 番目の人は同じタイムスロットを選択できません)。プロバイダー側​​でこれらの予定を 1 つの Google カレンダーにプッシュするように要求しました。Google アカウントとそのカレンダーを作成し、この同じ Google ユーザーの下で Calendar API にアクセスできる API キーを作成しました。そのため、Web サイトで毎回このユーザーの資格情報を使用してイベントを作成したいと考えています。これは「サービス アカウント」のように見えますが、アプリケーションを作成したユーザーでさえ、ユーザー データにアクセスできないようです。

これをやってのける方法についてのアイデアはありますか?それが驚くほど単純であるように思われ、私がこのようなことをしたいと思った最初の人である方法はありませんが、その例を見つけることができれば気になります。

ここにコードのスニペットがあります

$event = new Google_Event();
$event->setSummary($title);
$event->setLocation($location);
$start = new Google_EventDateTime();
$start->setDateTime($date . 'T' . $startTime . ':00.000-06:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime($date . 'T' . $endTime . ':00.000-06:00');
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail($email);
$attendees = array($attendee1);
$event->attendees = $attendees;

$client = new Google_Client();
$service = new Google_CalendarService($client);

$createdEvent = $service->events->insert('my calendar ID', $event);

そしてエラー

Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/projecthimcal@gmail.com/events?key=AIzaSyAfSCfLJCMSkGRmjZXRtChPPcMNmEuCZow: (401) Login Required' in /home/mydomain.com/wp-content/themes/mytheme/libs/gAPI/io/Google_REST.php:66
4

1 に答える 1

1

少し遅すぎるかもしれませんが、認証を設定する必要があります。

これが私が使用したコードです。まだこれを探している人に役立つことを願っています(クラス名があなたのものと異なる場合がありますが、ロジックは同じであるAPI PHPクライアントを使用したことに注意してください):

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';
    session_start();

    $client = 新しい Google_Client();
    $client->setApplicationName("Google カレンダー PHP スターター アプリケーション");

    // https://code.google.com/apis/console?api=calendar にアクセスして、
    // クライアント ID、クライアント シークレット、およびリダイレクト URI を登録します。
    $client->setClientId('');
    $client->setClientSecret('');
    $client->setRedirectUri('');
    $client->setDeveloperKey('');
    $client->setScopes(array(
        'https://www.googleapis.com/auth/plus.me',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/calendar',
        「https://www.googleapis.com/auth/calendar.readonly」
    ));

    $cal = 新しい Google_Service_Calendar($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']);
    }

    if ($client->getAccessToken()) {            
        $event = 新しい Google_Service_Calendar_Event();

        $event->setSummary($title);
        $event->setLocation($location);

        $start = new Google_Service_Calendar_EventDateTime();
        $start->setTimeZone('アメリカ/モントリオール');
        $start->setDateTime($date . 'T' . $startTime . ':00.000-06:00');
        $event->setStart($start);

        $end = new Google_Service_Calendar_EventDateTime();
        $end->setTimeZone('アメリカ/モントリオール');
        $end->setDateTime($date . 'T' . $endTime . ':00.000-06:00');
        $event->setEnd($end);


        $attendee1 = new Google_Service_Calendar_EventAttendee();
        $attendee1->setEmail($email);
        $attendees = array($attendee1);
        $event->attendees = $attendees;

        $cal->events->insert($email, $event);

        $_SESSION['token'] = $client->getAccessToken();

    } そうしないと {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect me!</a>";
    }

于 2014-05-21T17:37:31.677 に答える