public async void BatchInsertFromTemplate(int UserId, string Timezone, List <WeeklyTemplateEventData> fromTemplateEvents) { string AccessToken = oAuthService.GetGoogleAccessToken(UserId); if (AccessToken != null && fromTemplateEvents.Count != 0) { System.Diagnostics.Debug.WriteLine("Calendar has FromTemplate events!... attemping Google batch insert!"); string[] scopes = new string[] { "https://www.googleapis.com/auth/calendar" }; var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = oAuthService.GoogleClientId, ClientSecret = oAuthService.GoogleClientSecret }, Scopes = scopes, DataStore = new FileDataStore("Store") }); var token = new TokenResponse { AccessToken = AccessToken, RefreshToken = oAuthService.GetGoogleRefreshToken(UserId) }; var credential = new UserCredential(flow, UserId.ToString(), token); var service = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = oAuthService.GoogleApplicationName, }); var request = new BatchRequest(service); foreach (WeeklyTemplateEventData template in fromTemplateEvents) { request.Queue <Event>(service.Events.Insert( new Event { Summary = template.Title, Start = new EventDateTime() { DateTimeRaw = template.StartDateTime, TimeZone = Timezone }, End = new EventDateTime() { DateTimeRaw = template.EndDateTime, TimeZone = Timezone }, ColorId = exCalHelperService.GetGoogleColor(template.CategoryId) }, "primary"), (content, error, i, message) => { exCalHelperService.StoreExternalEventId(UserId, template.LocalId, content.Id, 1, content.ETag); }); } await request.ExecuteAsync(); } }
public void BatchInsertFromTemplate(int UserId, string Timezone, List <WeeklyTemplateEventData> fromTemplateEvents) { string CalendarApi = "https://graph.microsoft.com/v1.0/$batch"; string AccessToken = oAuthService.GetMicrosoftAccessToken(UserId); string respObject; if (AccessToken != null && fromTemplateEvents.Count != 0) { HttpWebRequest createRequest = (HttpWebRequest)WebRequest.Create(CalendarApi); createRequest.Method = "POST"; createRequest.ContentType = "application/json"; createRequest.Headers["authorization"] = AccessToken; List <MsBatchRequest.PostContainer> BatchPostCollection = new List <MsBatchRequest.PostContainer>(); foreach (WeeklyTemplateEventData template in fromTemplateEvents) { MsBatchRequest.PostContainer batchEntry = new MsBatchRequest.PostContainer { Id = template.LocalId.ToString(), Url = "/me/calendar/events", Method = "POST", Headers = new MsBatchRequest.MsHeaders { Content_Type = "application/json" }, Body = new MsBatchRequest.PostBody { Subject = template.Title, Start = new CalendarDateTimeZone { DateTime = template.StartDateTime, TimeZone = Timezone }, End = new CalendarDateTimeZone { DateTime = template.EndDateTime, TimeZone = Timezone }, Categories = new[] { exCalHelperService.GetMicrosoftColor(template.CategoryId) } } }; BatchPostCollection.Add(batchEntry); } MsBatchRequest.PostRequestsContainer BatchPostRequests = new MsBatchRequest.PostRequestsContainer { Requests = BatchPostCollection }; var json = JsonConvert.SerializeObject(BatchPostRequests); try { using (var streamWriter = new StreamWriter(createRequest.GetRequestStream())) { streamWriter.Write(json); } var httpResponse = (HttpWebResponse)createRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { respObject = streamReader.ReadToEnd(); } var batchResponse = MsBatchResponse.FromJson(respObject); foreach (Response postResp in batchResponse.Responses) { exCalHelperService.StoreExternalEventId(UserId, Convert.ToInt32(postResp.Id), postResp.Body.Id, 2, postResp.Body.OdataEtag); } } catch (WebException ex) { string resp; using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) { resp = streamReader.ReadToEnd(); } System.Diagnostics.Debug.WriteLine("Failed to BATCH INSERT/POST"); System.Diagnostics.Debug.WriteLine(resp); } } }