public async void BatchDeleteFromTemplateEvents(int UserId, List <ExternalEventIds> externalEventIds) { string AccessToken = oAuthService.GetGoogleAccessToken(UserId); if (AccessToken != null && externalEventIds.Count != 0) { 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 (ExternalEventIds template in externalEventIds) { if (template.GoogleId != null) { request.Queue <Event>(service.Events.Delete("primary", template.GoogleId), (content, error, i, message) => { exCalHelperService.DeleteExternalEvent(template.GoogleId); }); } } await request.ExecuteAsync(); } }
public void BatchDeleteFromTemplateEvents(int UserId, List <ExternalEventIds> externalEventIds) { string CalendarApi = "https://graph.microsoft.com/v1.0/$batch"; string AccessToken = oAuthService.GetMicrosoftAccessToken(UserId); string respObject; if (AccessToken != null && externalEventIds.Count != 0) { HttpWebRequest createRequest = (HttpWebRequest)WebRequest.Create(CalendarApi); createRequest.Method = "POST"; createRequest.ContentType = "application/json"; createRequest.Headers["authorization"] = AccessToken; List <MsBatchRequest.DeleteEventContainer> BatchDeleteCollection = new List <MsBatchRequest.DeleteEventContainer>(); foreach (ExternalEventIds template in externalEventIds) { if (template.MicrosoftId != null) { MsBatchRequest.DeleteEventContainer batchEntry = new MsBatchRequest.DeleteEventContainer { Id = template.LocalId.ToString(), Url = "/me/calendar/events/" + template.MicrosoftId, Method = "DELETE", Headers = new MsBatchRequest.MsDeleteHeaders { Content_Type = "application/x-www-form-urlencoded", Content_Length = "0" } }; BatchDeleteCollection.Add(batchEntry); exCalHelperService.DeleteExternalEvent(template.MicrosoftId); } } MsBatchRequest.DeleteRequestsContainer BatchPostRequests = new MsBatchRequest.DeleteRequestsContainer { Requests = BatchDeleteCollection }; 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 serverResp = (JObject)JsonConvert.DeserializeObject(respObject); System.Diagnostics.Debug.WriteLine(serverResp); } catch (WebException ex) { string resp; using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) { resp = streamReader.ReadToEnd(); } System.Diagnostics.Debug.WriteLine("Failed to BATCH DELETE"); System.Diagnostics.Debug.WriteLine(resp); } } }