문제

Basic rundown: I've written a client side calendar that allows people to schedule an appointment and reserve a timeslot (e.g. reservations are logged in a database and a second person can't choose the same time slot) for ease of syndication and printing of these appointments at the provider side they've requested that I push these events to a single google calendar. I've created a google account and a calendar for it, then created API key with access to the Calendar API under this same google user. So I want my website to use this user's credentials every time to create the events. It seems like this would be a "service account", however that doesn't seem to have access to user data, not even the user that created the application.

Any ideas on how to pull this off? If seems like it should be shockingly simple and that there's no way I'm the first person to want to do something like this, but damned if I can find any examples of it.

Here's a snippet of the code

$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);

and the error

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
도움이 되었습니까?

해결책

Maybe it is a bit too late... but you have to setup an authentification.

Here is the code I used for mine, hope it can helps people still looking for this (Note that I used the api PHP client the class name may differ from yours but the logic is still the same):

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

    $client = new Google_Client();
    $client->setApplicationName("Google Calendar PHP Starter Application");

    // Visit https://code.google.com/apis/console?api=calendar to generate your
    // client id, client secret, and to register your redirect 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 = new 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 = new Google_Service_Calendar_Event();

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

        $start = new Google_Service_Calendar_EventDateTime();
        $start->setTimeZone('America/Montreal');
        $start->setDateTime($date . 'T' . $startTime . ':00.000-06:00');
        $event->setStart($start);

        $end = new Google_Service_Calendar_EventDateTime();
        $end->setTimeZone('America/Montreal');
        $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();

    } else {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect me!</a>";
    }

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top