c# - Google Calendar Api v3 asp.net Code 200, yet no event inserted -
hello,
i building appointment finder in asp.net core. managed code 200 when accessing calendar.
however, event want send not saved google calendar. console dashboard shows it's received. there 1 calendar in account.
my code 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 class that's creating event several methods. tried using event class itself, outcome same.
the service created 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 service. calendarservice service = new calendarservice(new baseclientservice.initializer() { httpclientinitializer = credential, //applicationname = "showcase", }); return service; }
on cshtml page, razor:
calendarconnection testconnection = new calendarconnection(); testconnection.insertintocalendar();
i getting code 200, event not insterted calendar. read perl article this, cannot make out of it.
what doing wrong?
thank in advance.
edit: when call events.list console application, can see 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 see them in calendar?
i found error.
the problem wasn't code actually.
first of all, if use "primary" calendar id, somehow not calendar id of account. can events it, it's not shown in calendar. colleague suggested it's kind of sandbox calendar (or bug).
after entered calendar id account (either e-mail address primary or address find in calendar settings), got error 403.
this caused not sharing calendar service account adress (an andress created google while creating service account, xxxxx@xxxxx.iam.gserviceaccount.com)
so, if wanna create events through google calendar:
create service account @ https://console.developers.google.com
share calendar service account's e-mail address.
use code above (or 1 uses client_secrets.json)
note: not code, administration work missing.
code here:
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 = "xxxxxx@group.calendar.google.com"; // or e-mail address of google account if want use primary calendar eventsresource.insertrequest request = service.events.insert(newevent, calendarid); event createdevent = request.execute(); }
Comments
Post a Comment