0

I am trying to send events to google calender using api from php. but there is some error always with this. cannot understand what to do next. Here is my code:

            require_once './gplus-verifytoken-php-master/
            google-api-php-client/src/Google_Client.php';
            require_once '
            ./gplus-verifytoken-php-master/
            google-api-php- client/src/contrib/Google_CalendarService.php';

            session_start();

            ob_start();
            $client = new Google_Client();
            $client->setApplicationName('demo');
            $client->
            setClientId('client id');
            $client->setClientSecret('secret');
            $client->setRedirectUri('http://someurl.com');
            $client->
            setDeveloperKey('dev key');
            $cal = new Google_CalendarService($client);

            $event = new Google_Event();
            $event->setSummary('Pi Day');
            $event->setLocation('Math Classroom');
            $start = new Google_EventDateTime();
            $start->setDateTime('2016-11-14T10:00:00.000-05:00');
            $event->setStart($start);
            $end = new Google_EventDateTime();
            $end->setDateTime('2016-11-14T10:25:00.000-05:00');
            $event->setEnd($end);

            // error is on this next line
           $createdEvent = 
           $cal->events->insert('some_calendar@gmail.com',$event);

            echo $createdEvent->id;

            ?>

Please help. Thank you.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

2 Answers2

0

The first thing I am noticing here is that you are not authenticating your API call and that is why you are probably getting the error. You must first authenticate the user to access user data. Please refer to the documentation here https://developers.google.com/api-client-library/php/auth/web-app. After the user is successfully authenticated then you can make the API call. Another thing I noticed is that you are putting the email address on the calendar id. If you want to access the primary calendar of the currently logged in user, use the "primary" keyword. Your code should look something like this:

session_start();

$client = new Google_Client();
$client->setAuthConfig("client_secrets.json");
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/event.php');
$client->addScope("https://www.googleapis.com/auth/calendar");

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

    $client->setAccessToken($_SESSION['access_token']);

    $cal = new Google_Service_Calendar($client);

    $event = new Google_Service_Calendar_Event(array(
        'summary' => 'Pi Day',
        'location' => 'Math Classroom',
        'description' => 'Pi History in detail',
        'start' => array(
            'dateTime' => '2016-11-14T10:00:00-05:00'   
        ),
        'end' => array(
            'dateTime' => '2016-11-14T10:25:00-05:00'
        ),  
        'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
            array('method' => 'email', 'minutes' => 24 * 60),
            array('method' => 'popup', 'minutes' => 10),
        ),
      ),
    ));

    $calendarId = 'primary';
    $event = $cal->events->insert($calendarId, $event);
    printf('Event created: %s\n', $event->htmlLink);

} else {

    if (!isset($_GET['code'])) {    

          $auth_url = $client->createAuthUrl();
          header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    } else {  

      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();

      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/event.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

    }

}

I hope you find this information helpful. I also recommend you reading the reference documentation found here https://developers.google.com/google-apps/calendar/v3/reference/events/insert

Morfinismo
  • 4,985
  • 4
  • 19
  • 36
-1

refer this below link and must read each functions descriptions with its usage:

https://developers.google.com/google-apps/calendar/create-events

below is sample code of google-api-link

// Refer to the PHP quickstart on how to setup the environment:
// https://developers.google.com/google-apps/calendar/quickstart/php
// Change the scope to Google_Service_Calendar::CALENDAR and delete any stored
// credentials.

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google\'s developer products.',
  'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'lpage@example.com'),
    array('email' => 'sbrin@example.com'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);
Soni Vimalkumar
  • 1,449
  • 15
  • 26
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/14258153) – Jiri Tousek Nov 11 '16 at 10:42
  • the reference link is belong the google and as far as i know they will properly take care of that in future if they change the link for API explanation. – Soni Vimalkumar Nov 11 '16 at 10:56