Google Calendar Api v3 EventDateTime

3,371 views
Skip to first unread message

kashif zafar

unread,
Apr 19, 2013, 11:49:23 AM4/19/13
to google-ca...@googlegroups.com
Hello,

I am trying to insert event in google calendar using google api v3 and getting error during insertion.I am using c#.

Error:

Google.Apis.Requests.RequestError

Invalid or mismatching start and end times. [400]

Errors [

    Message[Invalid or mismatching start and end times.] Location[ - ] Reason[invalid] Domain[global]

]

My code for EventDateTime is here.

                EventDateTime EventStartDTime = new EventDateTime();
                EventStartDTime.Date = "2013-06-03";
                EventStartDTime.DateTime = "2013-06-03T10:00:00.000+05:00";
                EventStartDTime.TimeZone = "Asia/Karachi";

                EventDateTime EventEndtDTime = new EventDateTime();
                EventEndtDTime.Date = "2013-06-03";
                EventEndtDTime.DateTime = "2013-06-05T10:00:00.000+05:00";
                EventEndtDTime.TimeZone = "Asia/Karachi";




Can Anyone help me to solve this issue?
Thanks in advance.




Howard Smith

unread,
Apr 19, 2013, 6:08:12 PM4/19/13
to google-ca...@googlegroups.com
I'm using Java. See my code below for adding event and continue reading my response 'below'.

    private void addEventToCalendar(GoogleCalendarEvent eventToAdd) throws Exception {
        try {

            Event event = new Event();
            event.setSummary(eventToAdd.getSummary());
            event.setDescription(eventToAdd.getDescription());
            event.setLocation(eventToAdd.getLocation());
            
            DateTime startDateTime = new DateTime(eventToAdd.getStart(), TimeZone.getTimeZone("UTC"));
            event.setStart(new EventDateTime().setDateTime(startDateTime));
            
            DateTime endDateTime = new DateTime(eventToAdd.getEnd(), TimeZone.getTimeZone("UTC"));
            event.setEnd(new EventDateTime().setDateTime(endDateTime));
            
            oAuthRequestAccess();
            delayIfNecessaryAndUpdateCounter();
            client.events().insert(calendarEntry.getId(), event).execute();

        } catch (GoogleJsonResponseException googleJsonException) {
            addEventToLog(eventToAdd);
            String msg = "caught the following exception: " +
                         googleJsonException.getDetails().toPrettyString();
            logger.info(msg);
            throw googleJsonException;
        } catch (Exception e) {
            addEventToLog(eventToAdd);
            String msg = "caught the following exception: " +
                         (e.getMessage() != null ? e.getMessage() : e.getLocalizedMessage());
            logger.info(msg);
            throw e;
        }
    }

I think I replied to another topic in this list. How often do you search the list before-or-instead-of asking questions? Research is a powerful tool and you can find an answer on your own 'quicker' than waiting for someone to kindly respond.

Also, please feel free to see more of my code at the URL below; you can refer there for 'later' reference.

kashif zafar

unread,
Apr 21, 2013, 5:52:38 AM4/21/13
to google-ca...@googlegroups.com
Thanks for your reply.I want to know  what is wrong in my code.I have seen java example before, this is not helpful for me.

James Covington

unread,
Apr 21, 2013, 12:42:34 PM4/21/13
to google-ca...@googlegroups.com
Looking at your code, then at the javadocs for the EventDateTime class, it looks like you are incorrectly setting the Date property for your two objects. They are only to be used if it is an all-day event. At any rate, your endtime object has conflicting settings. The Date is being set to 2013-06-03, while the date part of the DateTime is being set to 2013-06-05.
Message has been deleted
Message has been deleted

kashif zafar

unread,
Apr 22, 2013, 4:22:18 AM4/22/13
to google-ca...@googlegroups.com
Sorry,there was a mistake in writing in my post, the right code is here that i have tried.



                EventDateTime EventStartDTime = new EventDateTime();
                EventStartDTime.Date = "2013-06-03";
                EventStartDTime.DateTime = "2013-06-03T10:00:00.000+05:00";
                EventStartDTime.TimeZone = "Asia/Karachi";

                EventDateTime EventEndtDTime = new EventDateTime();
                EventEndtDTime.Date = "2013-06-05";

Howard W. Smith, Jr.

unread,
Apr 22, 2013, 7:47:58 AM4/22/13
to google-ca...@googlegroups.com
Is there any reason why you are 'writing' your code here instead of copying and pasting your code here?

James, can you recommend how to capture the google calendar API 'request', so it can be seen in Kashif's IDE console or server logs?


--
You received this message because you are subscribed to a topic in the Google Groups "Google Calendar API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-calendar-api/mfSU-y9NP_U/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to google-calendar...@googlegroups.com.
To post to this group, send email to google-ca...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-calendar-api?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

kashif zafar

unread,
Apr 22, 2013, 8:33:07 AM4/22/13
to google-ca...@googlegroups.com
Thanks Howard for your response

Two more things i want to tell you that i am working on windows form application and using service account for OAuth2.

Howard W. Smith, Jr.

unread,
Apr 22, 2013, 8:39:46 AM4/22/13
to google-ca...@googlegroups.com
On Mon, Apr 22, 2013 at 8:33 AM, kashif zafar <itskash...@gmail.com> wrote:

Two more things i want to tell you that i am working on windows form application and using service account for OAuth2.

It is obvious that you are making the connection via OAuth2, successfully. Just need to debug the requests you send to google calendar via google calendar API.

James Covington

unread,
Apr 22, 2013, 10:30:25 AM4/22/13
to google-ca...@googlegroups.com
As I stated earlier, I don't think you should be setting both the Date and DateTime. What happends if you modify as follows:
                EventDateTime EventStartDTime = new EventDateTime();
                EventStartDTime.DateTime = "2013-06-03T10:00:00.000+05:00";
                EventStartDTime.TimeZone = "Asia/Karachi";

                EventDateTime EventEndtDTime = new EventDateTime();
                EventEndtDTime.DateTime = "2013-06-05T10:00:00.000+05:00";
                EventEndtDTime.TimeZone = "Asia/Karachi";

kashif zafar

unread,
Apr 22, 2013, 11:15:45 AM4/22/13
to google-ca...@googlegroups.com
Now it returns following exception:

Google.Apis.Requests.RequestError

Forbidden [403]

Errors [

    Message[Forbidden] Location[ - ] Reason[forbidden] Domain[global]

]

kashif zafar

unread,
Apr 22, 2013, 11:22:01 AM4/22/13
to google-ca...@googlegroups.com
Here is my complete code:

try
            {

                X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

                var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
                {
                    ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
                    Scope = CalendarService.Scopes.Calendar.GetStringValue(),
                };
                var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
                var service = new CalendarService(new Google.Apis.Services.BaseClientService.Initializer()
                {
                    Authenticator = auth
                });
                Event objEvent = new Event();


                EventDateTime EventStartDTime = new EventDateTime();
                EventStartDTime.DateTime = "2013-06-03T10:00:00.000+05:00";
                EventStartDTime.TimeZone = "Asia/Karachi";

                EventDateTime EventEndtDTime = new EventDateTime();
                EventEndtDTime.DateTime = "2013-06-05T10:00:00.000+05:00";
                EventEndtDTime.TimeZone = "Asia/Karachi";

                objEvent.Description = "Testing";
                objEvent.Location = "Lahore";
                objEvent.Summary = "Testing";

                objEvent.Start = EventStartDTime;
                objEvent.End = EventEndtDTime;
               
                Event oEvent = service.Events.Insert(objEvent,"itskash...@gmail.com").Fetch();
              


            }
            catch (GoogleApiRequestException e)
            {
                RequestError error = e.RequestError;

            }

Howard W. Smith, Jr.

unread,
Apr 22, 2013, 11:30:00 AM4/22/13
to google-ca...@googlegroups.com
Is the following a valid timezone or a Google-Calendar-API valid timezone?

TimeZone = "Asia/Karachi"




            }

--

Howard W. Smith, Jr.

unread,
Apr 22, 2013, 11:32:55 AM4/22/13
to google-ca...@googlegroups.com
I wonder if the error is caused by the value you're assigning to objEvent.Location. I am sure that my endusers populate 'invalid' locations, and my web app simply assigns user-entered data in that Location, but I've done my best to ensure that they derive their 'data' via/from Google Maps JavaScript API (web page that I created that has geocoding).

kashif zafar

unread,
Apr 22, 2013, 11:42:13 AM4/22/13
to google-ca...@googlegroups.com

Yes "Asia/Karachi" is a valid time zone, we can skip this because it is not mandatory while the timezone offset is present with datetime.

James Covington

unread,
Apr 22, 2013, 11:44:32 AM4/22/13
to google-ca...@googlegroups.com
have you shared your calendar with the service account email address with management permissions?

kashif zafar

unread,
Apr 22, 2013, 11:57:36 AM4/22/13
to google-ca...@googlegroups.com
Not sure, how can i confirm that?

kashif zafar

unread,
Apr 23, 2013, 5:38:58 AM4/23/13
to google-ca...@googlegroups.com
Event insertion is working fine in APIs Explorer, I have added an event successfully.It means there is something wrong in my code not in calendar sharing settings.

Here is the response from APIs Explorer:

200 OK
- Show headers -
{
"kind": "calendar#event",
"etag": "\"GZxpEFttRDAOmLHnWRxLHHWPGwk/Z2NhbDAwMDAxMzY2NzA5NDgwOTIzMDAw\"",
"id": "k8k2p5qm2o3oa9o8ea9peud358",
"status": "confirmed",
"created": "2013-04-23T09:31:20.000Z",
"updated": "2013-04-23T09:31:20.923Z",
"location": "Lahore",
"creator": {
"displayName": "kashif zafar",
"self": true
},
"organizer": {
"self": true
},
"start": {
"dateTime": "2013-06-03T10:10:00+05:00"
},
"end": {
"dateTime": "2013-06-03T10:15:00+05:00"
},
"sequence": 0,
"reminders": {
"useDefault": true
}
}



On Monday, 22 April 2013 20:44:32 UTC+5, James Covington wrote:

kashif zafar

unread,
Apr 23, 2013, 5:45:01 AM4/23/13
to google-ca...@googlegroups.com
And i have got the list of events from my calendar with default calendar settings, but still not able to insert event.



On Monday, 22 April 2013 20:44:32 UTC+5, James Covington wrote:

kashif zafar

unread,
Apr 23, 2013, 7:09:20 AM4/23/13
to google-ca...@googlegroups.com
Thanks GOD.

I solved it, and the issue was not code, It works fine.

The problem was sharing my calendar. Something that I don't knew.

To allow edit your calendar events from service account, do not have enough with all steps you do in the Google Api Console, you have to give permission from your calendar configuration (Calendar configuration -> Share this calendar) to the email account specified into Google Api Console (email adress).

Howard W. Smith, Jr.

unread,
Apr 23, 2013, 8:03:42 AM4/23/13
to google-ca...@googlegroups.com

Correct, and yes, thank God!

When I first added google calendar API to my app, I learned /recognized that I had to share calendar to the google acct mentioned in the api console as well.

Thanks for sharing. Others will benefit from this email thread. :-)

--

James Covington

unread,
Apr 23, 2013, 8:19:08 AM4/23/13
to google-ca...@googlegroups.com
I ran into the same issue myself when first attempting to use the v3 API with service accounts. It is NOT well documented! Glad to hear you are resolved.

kashif zafar

unread,
Apr 23, 2013, 9:11:58 AM4/23/13
to google-ca...@googlegroups.com
Thanks both of you !

Bjørn Fridal

unread,
Aug 31, 2013, 6:34:54 PM8/31/13
to google-ca...@googlegroups.com
@Kashif Thanks mate. This is the first timeI have seen this mentioned. 

Brian Gray

unread,
Sep 1, 2013, 12:45:15 PM9/1/13
to google-ca...@googlegroups.com

I've tried to recreate your results, but the program throws an exception (400) on the first attempt to use the calendar service object.  I've tried to insert an event and to read the calendar list.  It looks like the certificate, provider, and auth objects are being created correctly (based on looking at values in the debugger).  

I know the service account is valid because a 3-party program is using it.  I may be running into a problem with DLL version mismatches.

What version of the Calendar, Authentication.OAuth2, and DotNetOpenAuth DLLs are you using?

bkg
Reply all
Reply to author
Forward
0 new messages