Hello,
I am currently building an appointment finder in asp.net core. I managed to get a code 200 when accessing the calendar.
However, the event I want to send is not saved by Google Calendar. The console dashboard shows it's received. There is only one calendar in the account.
My code so far:
public void InsertIntoCalendar()
{
var Cal = new CalendarEvents();
var newEvent = Cal.CreateEvent("TestEvent", "RandomLocation", "Description", "2017-07-28");
var service = CreateService();
service.Events.Insert(newEvent, serviceAccountEmail).Execute();
String calendarId = "primary";
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();
}
CalendarEvents is a class that's creating an Event by several methods. I tried using the Event class itself, but the outcome is the same.
The Service is created like this:
private CalendarService CreateService()
{
string keyFilePath = "random.p12;
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = Scopes
}.FromCertificate(certificate));
// Create the service.
CalendarService service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
//ApplicationName = "ShowCase",
});
return service;
}
On the cshtml page, I do this with razor:
CalendarConnection TestConnection = new CalendarConnection();
TestConnection.InsertIntoCalendar();
I am getting a code 200, but the event is not insterted into the calendar. I read the perl article about this, but I cannot make anything out of it.
What am I doing wrong?
Thank you in advance.
Edit: When I call events.list from a console application, I can see the events:
TEST (26.07.2017 20:58:08)
TEST (26.07.2017 20:58:57)
TEST (26.07.2017 21:03:38)
TEST (26.07.2017 21:05:27)
Why don't I see them in the calendar?
I found the error.
The problem wasn't my code actually.
First of all, if you use "primary" as calendar id, it somehow is not the calendar id of my account. You can get events from it, but it's not shown in your calendar. A colleague suggested it's kind of a sandbox calendar (or a bug).
After I entered the calendar id from the Account (either the e-mail address for your primary or an address you find in the calendar settings), I got an error 403.
This was caused by not sharing the calendar with the service account adress (an andress that is created by Google while creating a service account, usually like XXXXX@xxxxx.iam.gserviceaccount.com)
So, if you wanna create events through Google Calendar:
Create a service account at https://console.developers.google.com
Share your calendar with the service account's e-mail address.
Use the code above (or one that uses a client_secrets.json)
Note: It is not always your code, but some administration work that is missing.
Code here: