Home » Android » How to add event on google calendar using calendar API using android?

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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>";
    }
?>