Asked by:
Google Calender API with ASP.NET

Question
-
User-1100884601 posted
Hi,
I create one project with asp.net web-form to invoke google calendar API and insert and update events on google calendar. My Code works fine in development server but I am facing problem when i host in IIS . Problem is related to Access-token and Authorization in IIS.
As per google console, I know first i need to authorize and then exchange the code for token.
Here is my code
public void ImportGoogleEvents(string EventSummary, string EventLocation, string EventDescription, DateTime EventStartDateTime, DateTime EventEndDateTime, string CustomCalenderName, [Optional] string StartDatetimeZone, [Optional] string EndDatetimeZone, [Optional] string attachmentsfileUrl) { ClientSecrets secrets = new ClientSecrets() { ClientId = clientId, ClientSecret = clientSecret }; var valid_token = "VALID TOKEN HERE"; // This i got from Google PlayGround and it works fine but when i close Google Playground or Token Expires it starts generating error. var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse() { AccessToken = valid_token, ExpiresInSeconds = 3600, IssuedUtc = DateTime.Now }; var apiFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret } }); UserCredential credential = new UserCredential(apiFlow, "User Id here", token); bool success = credential.RefreshTokenAsync(CancellationToken.None).Result; token = credential.Token; if (credential != null) { var calendarService = new Google.Apis.Calendar.v3.CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName } ); if (calendarService != null) { var list = calendarService.CalendarList.List().Execute(); var listcnt = list.Items; var calendar = list.Items.SingleOrDefault(c => c.Summary == CustomCalenderName.Trim()); if (calendar != null) { Google.Apis.Calendar.v3.Data.Event calendarEvent = new Google.Apis.Calendar.v3.Data.Event(); calendarEvent.Summary = EventSummary; calendarEvent.Location = EventLocation; calendarEvent.Description = EventDescription; calendarEvent.Start = new Google.Apis.Calendar.v3.Data.EventDateTime { DateTime = EventStartDateTime /*new DateTime(StartDate.Year, StartDate.Month, StartDate.Day, StartDate.Hour, StartDate.Minute, StartDate.Second)*/, TimeZone = StartDatetimeZone }; calendarEvent.End = new Google.Apis.Calendar.v3.Data.EventDateTime { DateTime = EventEndDateTime /*new DateTime(EndDate.Year, EndDate.Month, EndDate.Day, EndDate.Hour, EndDate.Minute, EndDate.Second)*/, TimeZone = EndDatetimeZone }; calendarEvent.Recurrence = new List<string>(); var newEventRequest = calendarService.Events.Insert(calendarEvent, calendar.Id); var eventResult = newEventRequest.Execute(); } } } }
I will be thankful if someone who already worked and can help me on this .
Thanks in Advance,
Tuesday, May 14, 2019 2:33 PM
All replies
-
User-893317190 posted
Hi udal_cse ,
Does iis show any exception?
You could refer to the link below for using google calendar api
https://stackoverflow.com/questions/54066564/google-calendar-api-with-asp-net
Please refer to similar problem happens when the project is deployed to iis.
Please also refer to google's official website for asp.net.
https://developers.google.com/calendar/quickstart/dotnet
Best regards,
Ackerly Xu
Wednesday, May 15, 2019 6:14 AM -
User-1100884601 posted
Hi Xu,
Thanks for your reply and guide me
I corrected the code and now getting this error
"You need to have writer access to this calendar" though i already given write permission.
Thanks
Udal
Wednesday, May 15, 2019 3:23 PM -
User-893317190 posted
Hi udal_cse,
You could refer to the link below for inserting an event. It uses client id and client secret
http://www.learnsharecorner.com/asp-net/insert-event-to-google-calendar-using-asp-net/
To generate client id and client secret , please go to
https://developers.google.com/calendar/quickstart/dotnet
and click enable the google calendar api
Best regards,
Ackerly Xu
Thursday, May 16, 2019 7:18 AM