0

I would like to create a sample application using Xamarin and google calendar.

My plan is to retrieve a list of public holidays using google calendar, for example: "en.usa#holiday@group.v.calendar.google.com"

Since, I'm new to Xamarin, I've tried to mimic the behavior from https://stackoverflow.com/a/19048193/3125120 answer:

com.google.api.services.calendar.Calendar client = null;
        credential = GoogleAccountCredential.usingOAuth2(mContext, CalendarScopes.CALENDAR);
        credential.setSelectedAccountName(mList.get(0));
        client = getCalendarService(credential);
        do {
            com.google.api.services.calendar.model.Events events;
            events = client.events().list("en.usa#holiday@group.v.calendar.google.com").setPageToken(pageToken).execute();
            onHolidayChecked(events.getItems()); //result return here (events.getItems())
            pageToken = events.getNextPageToken();
        } while (pageToken != null);

private com.google.api.services.calendar.Calendar getCalendarService(GoogleAccountCredential credential) {
    return new com.google.api.services.calendar.Calendar.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
}

Can someone propose a way to mimic the same behaviour?

Thanks in advance.

DeJaVo
  • 3,091
  • 2
  • 17
  • 32
  • 1
    Have you read this : https://github.com/xamarin/google-apis? – York Shen Jan 17 '18 at 03:56
  • Thanks for the reference, I tried it. the api is not maintained at all. I had to change several missing reference to make it compile, when trying to use the example in the link, lots of missing components which are no longer supported by Xamarin. – DeJaVo Jan 17 '18 at 17:15
  • 1
    I ended up using http://kayaposoft.com/enrico/ API – DeJaVo Jan 21 '18 at 07:17

1 Answers1

1

I tried below code and It is only returning List of holidays from my calendar.

var calendarsUri = CalendarContract.Events.ContentUri;
var cursor = Forms.Context.ContentResolver.Query(calendarsUri, null, null, null, null);

if (cursor.MoveToFirst())
            {
                do
                {
                      calendarDataList.Add(new Calendar()
                      {
                          Id = cursor.GetString(cursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.Id)),
                          CalendarDisplayName = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName)),
                          AccountName = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Calendars.InterfaceConsts.AccountType)),
                          Title = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Title)),
                          Description = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Description)),
                          Dtstart = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Dtstart)),
                          Dtend = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Dtend))                       
                      }); 
                 } while (cursor.MoveToNext()); 
             }

calendarDataList contains all the public holidays.

Mohit
  • 1,185
  • 2
  • 11
  • 25
  • I am trying to get all the data (events , reminder and meeting etc) saved in my calendar on android device. This piece of code is returning only the holidays. Let me know if you know how to get rest of the data :) – Mohit Jul 10 '20 at 08:40