0
    /* ####################################################################################################################################
                                            Calendar API
#################################################################################################################################### */

    private class InsertRequest extends AsyncTask<Void, Void, List<String>>{

        private com.google.api.services.calendar.Calendar AppService = null;
        private Exception AppLastError = null;

        InsertRequest(GoogleAccountCredential credentials){
            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

            AppService = new com.google.api.services.calendar.Calendar.Builder(
                    transport,jsonFactory,credentials
            ).setApplicationName("SmartStudy2").build();
        }
/**********************************************************************************************/
        @Override
        protected List<String> doInBackground(Void... voids) {
            try {
                return insertEventThroughAPI();
            } catch (Exception e){
                AppLastError = e;
                cancel(true);
                return null;
            }
        }
/**********************************************************************************************/
        private List<String> insertEventThroughAPI() throws Exception {

            /*DateTime now = new DateTime(System.currentTimeMillis());

            Event event = new Event()
                    .setSummary("jeremy event")
                    .setLocation("Panji goa")
                    .setDescription("test Event");
            EventDateTime start = new EventDateTime()
                    .setDateTime(now)
                    .setTimeZone("Asia/Kolkata");
            event.setStart(start);
            event.setEnd(start);
            String CalendarID = "primary";

            AppService.events().insert(CalendarID,event).execute();
            TextView tv = (TextView) findViewById(R.id.Tview1);
            tv.setText("hell yeah");*/

            Event event = new Event()
                    .setSummary("Google I/O 2015")
                    .setLocation("800 Howard St., San Francisco, CA 94103")
                    .setDescription("A chance to hear more about Google's developer products.");

            DateTime startDateTime = new DateTime("2016-12-28T09:00:00-07:00");
            EventDateTime start = new EventDateTime()
                    .setDateTime(startDateTime)
                    .setTimeZone("America/Los_Angeles");
            event.setStart(start);

            DateTime endDateTime = new DateTime("2016-12-31T17:00:00-07:00");
            EventDateTime end = new EventDateTime()
                    .setDateTime(endDateTime)
                    .setTimeZone("America/Los_Angeles");
            event.setEnd(end);

            String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"};
            event.setRecurrence(Arrays.asList(recurrence));


            EventReminder[] reminderOverrides = new EventReminder[] {
                    new EventReminder().setMethod("email").setMinutes(24 * 60),
                    new EventReminder().setMethod("popup").setMinutes(10),
            };
            Event.Reminders reminders = new Event.Reminders()
                    .setUseDefault(false)
                    .setOverrides(Arrays.asList(reminderOverrides));
            event.setReminders(reminders);

            String calendarId = "primary";
            event = AppService.events().insert(calendarId, event).execute();
            System.out.printf("Event created: %s\n", event.getHtmlLink());




            return null;
        }
/**********************************************************************************************/
        @Override
        protected void onPreExecute(){
            TextView tv = (TextView) findViewById(R.id.Tview1);
            tv.setText("In Progress");
            (MainActivity.this).insertProgress.show();
        }

        @Override
        protected void onPostExecute(List<String> output){
            (MainActivity.this).insertProgress.hide();
            TextView tv = (TextView) findViewById(R.id.Tview1);
            if (output == null || output.size() == 0)
                tv.setText("no result");
            else
                tv.setText(TextUtils.join("",output));


        }

        @Override
        protected void onCancelled(){
            (MainActivity.this).insertProgress.hide();
            TextView tv = (TextView) findViewById(R.id.Tview1);

            if(AppLastError != null){
                if(AppLastError instanceof GooglePlayServicesAvailabilityIOException){
                    showGooglePlayPlayServicesAvailabilityErrorDialog(
                            ((GooglePlayServicesAvailabilityIOException) AppLastError).getConnectionStatusCode()
                    );
                }
                else if (AppLastError instanceof UserRecoverableAuthIOException){
                    startActivityForResult(
                            ((UserRecoverableAuthIOException) AppLastError).getIntent(),
                            MainActivity.REQUEST_AUTHORIZAION
                            );
                } else {
                    tv.setText("the following error occured \n:"+AppLastError.getMessage());
                }
            } else {
                tv.setText("you cancelled");
            }
        }

    }

This insert event gets cancelled and returns a null exception i am doing all the nccessary checks that are needed like play services avaiability and checking device connectivity what am i doing wrong I have done this by following google calendar API documentation

PS: android calendar provider i not an alternative for me

jeremy
  • 1
  • 4
  • Check this code in link https://readyandroid.wordpress.com/add-event-and-reminder-to-calendar-android/ – Ready Android Dec 22 '16 at 08:36
  • OK just found out that this throws a GoogleAuthIOException – jeremy Dec 22 '16 at 08:41
  • the problem was caused by the debug keystore. I was using the wrong debug keystore. http://stackoverflow.com/questions/41355802/google-oauth2-authentication-error – jeremy Jan 06 '17 at 11:37

0 Answers0