Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert event to user google calendar using php?

I have these following codes to insert to my specific google calendar. Well it very successful, but how to let user can add to their own calendar? Somebody can help me... My expected results was like user can login via google.... it's means that user can add to their own google calendar. Thanks.

Codes to add to my specific calendar

require_once './vendor/google/apiclient/src/Google/autoload.php';

$key_file_location = 'Calendar-96992da17e2dda.p12'; // key.p12 to create in the Google API console

$client_id = '[email protected]';
$service_account_name = '[email protected]'; // Email Address in the console account

if (strpos($client_id, "gserviceaccount") == false || !strlen($service_account_name) || !strlen($key_file_location)) {
    echo "no credentials were set.";
    exit;
}

/** We create service access ***/
$client = new Google_Client();  

/************************************************
If we have an access token, we can carry on.  (Otherwise, we'll get one with the help of an  assertion credential.)
Here we have to list the scopes manually. We also supply  the service account
 ************************************************/
if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
array('https://www.googleapis.com/auth/calendar'), // ou calendar_readonly
$key
);

$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();



// Get the API client and construct the service object.
$service = new Google_Service_Calendar($client);


    /************* INSERT ****************/
$event = new Google_Service_Calendar_Event(array(
  'summary' => 'My Summary',
  'location' => 'My Location',
  'description' => 'My Description',
  'start' => array(
    'dateTime' => '2015-12-31T09:09:00',
    'timeZone' => 'Asia/Singapore',
  ),
  'end' => array(
    'dateTime' => '2015-12-31T17:16:00',
    'timeZone' => 'Asia/Singapore',
  ),
  'attendees' => array(
    array('email' => '[email protected]'),
    array('email' => '[email protected]'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$events = $service->events->insert('primary', $event);
printf('Event created: %s', $events->htmlLink);
like image 270
Nere Avatar asked Dec 31 '15 04:12

Nere


People also ask

Can I add events to someone else's Google Calendar?

Use a link to add a public calendar Important: You can only add a calendar with a link if the other person's calendar is public. Learn more about public calendars.

How do I add an event to API in Google Calendar?

In order to successfully create events, you need to: set your OAuth scope to https://www.googleapis.com/auth/calendar . ensure the authenticated user has write access to the calendar with the calendarId you provided (for example by calling calendarList. get() for the calendarId and checking the accessRole ).


1 Answers

Uses the PHP client library.

// 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' => '[email protected]'),
    array('email' => '[email protected]'),
  ),
  '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);

for more details please check official document from here Events: insert

like image 153
Ishan Shah Avatar answered Sep 19 '22 00:09

Ishan Shah