How to add event on google calendar using calendar API using android?

38,276

Solution 1

After Searching for some time i have finally find the solution .the answer was in the google document it self just go through this link

it shows how to create an event using google calender api.

Solution 2

This is such a giant pain in the ass - but I finally got it working for creating events at least.

Download the most recent Google PHP API zip, and upload it to your includes folder on your webserver. Use Google API Console to set up an API client. Make sure you set your redirect url to be the same as your page's url - so it redirects to its self.

I've initially just set some variables for event details, you can make a form which shoves these in if you want.

Here's my code:

<?php
    $jobname = "BINGO";
    $joblocation = "Your mums house";
    $jobdescription = "An interview with a dog.";
    $startofjob = "2013-12-20T17:00:00.000+00:00"; //datetimes must be in this format
    $endofjob = "2013-12-20T18:00:00.000+00:00"; // YYYY-MM-DDTHH:MM:SS.MMM+HH:MM
    //So that's year, month, day, the letter T, hours, minutes, seconds, miliseconds, + or -, timezoneoffset in hours and minutes



    include('google-api-php-client/src/Google_Client.php');
    include('google-api-php-client/src/contrib/Google_CalendarService.php');

    session_start();

    $client = new Google_Client();
    $client->setApplicationName('doesntmatter-whateveryouwant');
    $client->setClientId('yourclientid');
    $client->setClientSecret('yourclientsecret');
    $client->setRedirectUri('yourredirecturl-setingoogleconsole');
    $client->setDeveloperKey('yourdeveloperkey');
    $cal = new Google_CalendarService($client);

    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_Event();
    $event->setSummary($jobname);
    $event->setDescription($jobdescription);
    $event->setLocation($joblocation);
    $start = new Google_EventDateTime();
    $start->setDateTime($startofjob);
    $event->setStart($start);
    $end = new Google_EventDateTime();
    $end->setDateTime($endofjob);
    $event->setEnd($end);

    $createdEvent = $cal->events->insert('[email protected]', $event);
    echo $createdEvent->id;


    $_SESSION['token'] = $client->getAccessToken();
    } else {
      $authUrl = $client->createAuthUrl();
      print "<a class='login' href='$authUrl'>Connect Me!</a>";
    }
?>
Share:
38,276
Ramz
Author by

Ramz

Android Application Developer Technopark Trivandrum(2011-2013) Now Working for Voltella Co-Founder(2013 to present) And l love my Nephew Munnachiii and Niece Meenuttiii *Won the Google Biz Droid competition 2014 banglore(Google) *2nd place for Angelhack 2013 banglore (Microsoft) Github Profile gist profile LinkedIn My Posts

Updated on February 15, 2020

Comments

  • Ramz
    Ramz over 4 years

    Hi I am trying to create an event in Google calendar using Google calendar API in android.

    I have created a sample project provided by Google, and I followed the each steps and compiled the project successfully.

    But in this Example of Google calendar, I can only create a calendar name to my Google calendar account, I can't create any event.

    Is there any way to create an event in Google calendar? If so how can I do it?