lunedì 29 ottobre 2012

C# Google API - Retrieve Own Calendars List

I think google calendar is a very useful service. 
The following code shows how to get the list of our calendars, if we want to do our own program to getting them.


public void LoadCalendars()
 {
    //first i clear the ComboBox 
    cmb_calendars.Items.Clear();
    CalendarService service = new CalendarService("appName");
    service.setUserCredentials("yourMail@gmail.com", "yourPassword");

    CalendarQuery query = new CalendarQuery();

    query.Uri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
    CalendarFeed calendarFeed = (CalendarFeed)service.Query(query);

    foreach (CalendarEntry entry in calendarFeed.Entries)
    {
        // see the code down for the definition of the "ComboboxItem" object  
        ComboboxItem item = new ComboboxItem();
        item.Text = entry.Title.Text;

        //take only the part that refers to the calendar for future use
        item.Value = entry.Id.AbsoluteUri.Substring(55);

        cmb_calendars.Items.Add(item);
    }
 }


Here the definition of the "ComboboxItem" class used above to filling the combobox
class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }

** I want to remeber to go here http://support.google.com/mail/bin/answer.py?hl=it&answer=1173270 to get  Application-specific password

My Two Cents...

4 commenti:

  1. It's great.

    Now using Nuget:


    Install-Package Google.GData.Calendar

    New API:
    Install-Package Google.Apis.Calendar.v3

    I dont' know the differences neither good patterns and practices about it.

    RispondiElimina
  2. Now, I get 403 error about it.


    http://googleappsupdates.blogspot.com.es/2014/06/calendar-gdata-api-google-calendar.html


    Back in November 2011, we announced the deprecation of the Calendar GData API (v1, v2) when the APIv3 was made available. We’ll now be shutting down these older versions on November 17, 2014.

    Any service dependent on the Calendar GData API (v1, v2), such as the Google Calendar Connectors (GCC), will no longer work as intended. GCC users should update to use Calendar Interop or their own custom built connectors that sync with v3 of the API.

    RispondiElimina
  3. Migration guide:

    https://developers.google.com/google-apps/calendar/migration
    http://www.programmableweb.com/news/google-calendar-api-v1-and-v2-to-shut-down-nov-17/2014/10/27

    RispondiElimina