locked
Google Calender API with ASP.NET RRS feed

  • 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