views:

480

answers:

1

Hi, I'm trying to add an event to a specific calendar in google calendar and I just don't find how. Here's my code :

        CalendarService service = new CalendarService("MyTEst");
        service.setUserCredentials("Username", "Password");
        EventEntry entry = new EventEntry();

        // Set the title and content of the entry.
        entry.Title.Text = "title";
        entry.Content.Content = "test";

        // Set a location for the event.
        Where eventLocation = new Where();
        eventLocation.ValueString = "Location";
        entry.Locations.Add(eventLocation);

        When eventTime = new When(DateTime.now, DateTime.now.AddDays(2));
        entry.Times.Add(eventTime);

        Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/private/full");

        // Send the request and receive the response
        AtomEntry insertedEntry = service.Insert(postUri, entry);

Can anyone help me out with this one?

Edit

Maybe I should mention that this fonctionnability is only accessible for an administrator of a site which want to easly add rendez-vous and note to his google calendar so I automaticaly authenticated it with "hardcoded" value so I'm sure the username and password are ok.

+1  A: 

Your code is working with the default Google Calendar for your specified user name and password. (IE it is using the default calendar for [email protected]) You can see this because the URI points to "/feed/default/private". If you want to post the event to another calendar the user name must authorized to post to that calendar, and you need to post to THAT calendars private uri.

EDIT: The default format of this private URL is "http://www.google.com/calendar/feeds/CALENDAR_ID/private/full"

To find the calendar id, it is next Calendar Address in the calendar settings page on Google Calendars. It will appear similar to this:

"*************@group.calendar.google.com"

The final URL would be:

EDIT: "http://www.google.com/calendar/feeds/*************@group.calendar.google.com/private/full"

This will go in your Uri postUri = new Uri();

EDIT:

My mistake was that I mentioned that you need to also include the private key after the word private. You do not actually have to do this. I have verified that I could successfully post to a secondary calendar by removing the private key.

Daniel Joseph
Thank you very much DanJo519. I'll try this now !!
Simon
I've try and it still fails into an error ; Execution of request failed: google.com/calendar/feeds/[email protected]/private-PRIVATE_KEY/full. I'm not sur to understand. I've try with both "full" or "basic" at the end (Basic is provide by default from google but i've tried with full too cuz u said so. Anyway, no one works :(
Simon
I have made a modification to my answer. You do not actually need to include -PRIVATE_KEY. Removing that will allow you to post, assuming that you have write permissions on the calendar to begin with. I have successfully tested this with one of my secondary calendars.
Daniel Joseph
Thank you DanJo! It's now workin'!
Simon
Heum eu well, I tought it was working, but it was not. Always the same error ... But what do you mean with "assuming that you have write permissions on the calendar to begin with" ?
Simon
So then you are still seeing the "Execution of request failed"? When I said that you need write permissions on the calendar first, Google allows secondary calendars to be shared so that multiple people can add events to them. You need to make sure the calendar that you want to add an event to can have events added to it by the username you are passing to the CalendarService. You can do this by checking the calendar permissions on the sharing tab of the calendars settings in Google. The username that you set should have the "Make changes" permission.
Daniel Joseph
Yea sure, the user I'm using have "Make changes AND manage sharing" ... It's the "owner" user. Do I have to put the calendar "Public" even if I authenticate the access.
Simon
You should not have to. I have copied the code you provided above, replacing only the username, password, and the postUri. My calendar is not shared and accessible only to the user I'm authenticating with (who is also the user) and I am successfully able to post to that calendar. You may need to provide the full exception that results from this code if you are still having problems. Also, I noticed that you have DateTime.now instead of DateTime.Now (note capital N), however this would cause a compilation error, so I am guessing this isn't the problem.
Daniel Joseph
It is because i've replace values by writting it here in stackoverflow and forget to put an upper letter. Ordinary, it's from a database source. I will write the exact error here during the day.
Simon
WELL! I finaly succed to ... "access" to it. But there's still an error in innerException which says "This feed is read-only"... any ideas? Thank you again DanJo, you're realy helpfull. Note that my error with URI were that I've erased the "[...]/private/[...]" :P
Simon
Ho well now it is workin!! Lol again my fault. When you retrive ID from XLM, by default its "[...]/basic" at the end. So i replace it and now its workin! Thank you again dan!
Simon