Time difference issue when adding a Google Calenda

2019-08-14 08:30发布

问题:

I am using "Google.GData.Calendar" API to add Events to the Google Calendar using c#. But there is time difference between the events created in my DB and Google Calendar.

Following is the Code to add an Event to Google Calendar using the Google.GData API:

public static void AddEventToGoogle()
{
    //string timezone = "US Mountain Standard Time";

    Uri postUri = new Uri("https://www.google.com/calendar/feeds/default/private/full");
    try
    {
        GOAuthRequestFactory authFactory = new GOAuthRequestFactory("cl", "MyApp");
        authFactory.ConsumerKey =  ConfigurationManager.AppSettings["GConsumerKey"].ToString();
        authFactory.ConsumerSecret =  ConfigurationManager.AppSettings["GConsumerKeySecret"].ToString();
        authFactory.Token = "xxxxxxxxxx";
        authFactory.TokenSecret = "xxxxxx";
        Google.GData.Calendar.CalendarService service = new Google.GData.Calendar.CalendarService(authFactory.ApplicationName);
        service.RequestFactory = authFactory;

        EventEntry entry = new EventEntry();
        entry.Title.Text = "Test Google Event Create";
        entry.Content.Content = "Test Google Event Create";
        entry.Notifications = false;

        When eventTime = new When();
        eventTime.AllDay = false;
        eventTime.StartTime = new DateTime(2013, 5, 9, 8, 0, 0);
        eventTime.EndTime = new DateTime(2013, 5, 9, 11, 0, 0);
        entry.Times.Add(eventTime);

        // Send the request and receive the response:
        EventEntry insertedEntry = service.Insert(postUri, entry);
        if (insertedEntry != null && !string.IsNullOrEmpty(insertedEntry.EventId))
        {
            //Get the insertedEntry.EventId
        }
    }
    catch (GDataRequestException gre)
    {
        HttpWebResponse response = (HttpWebResponse)gre.Response;
    }
}

But with the above code there is a time difference with the event I intended to create and the entry in the Google Calendar. The TimeZone I have in my Google Calendar is "(GMT-07:00) Mountain Time - Arizona". This is happening when i change the TimeZones of my Google Calendar to CST, PST,...

Please suggest a workaround for this situation or where do I need to specify the TimeZone while adding an event to Google Calendar.

Updated my question with the complete method i used to add an event

回答1:

The GData library uses v2 of the Google APIs. I think you will need to migrate your code to v3 in order to get proper support for time zones on events.

For example, see this page which shows an example of creating a recurring event. (Switch to the ".NET" tab".

Here are some resources that may help you:

  • Migration Guide
  • What's new in v3 (it doesn't mention time zones though)
  • Client libraries (see the ".NET" tab)

I did look through your code and the reference guide to the v2 GData library. I only see TimeZone properties on the CalendarEntry, EventFeed, and EventQuery classes. Since you're not using any of those, I don't think it is possible in v2.

UPDATE

It is possible in the v2 API. You can pass a time zone in the ICAL formatted <gd:recurrence> tag. Take a look at the note following the example in this documentation.

However, it doesn't appear that it is exposed as a property of the EventEntry class in the .Net GData client library. So my recommendation stands - use the v3 api and client library.