Here is my code:
using System;
using System.Net;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using System.Linq;
namespace WebsiteTextExtractor {
class Program {
async static Task Main(string[] args) {
string websiteURL = "CensoredURLThatWorks";
WebClient client = new WebClient();
string websiteText = client.DownloadString(websiteURL);
//File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\ElieCours.ics", websiteText);
// Console.WriteLine($"ElieCours.ics Updated at {Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ElieCours.ics"}.");
string[] scopes = new string[] { CalendarService.Scope.Calendar };
var credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets {
ClientId = "[redacted]",
ClientSecret = "[redacted]"
},
scopes,
"MyClient",
CancellationToken.None
).Result;
var service = new CalendarService(new BaseClientService.Initializer() {
HttpClientInitializer = credentials,
ApplicationName = "CalendarCleaner"
});
var events = service.Events.List("fc47dab8b05e7b6d0e549ff3207b9f400adfb8ff96b50ecc4ed51a9ecc75ebbe@group.calendar.google.com").Execute().Items;
//Deleting all events
ParallelOptions parallel = new() {
MaxDegreeOfParallelism = 3,
};
Console.WriteLine(events.Count);
await Parallel.ForEachAsync(events, parallel, async (ev, token) => {
await Task.Delay(300, token);
await service.Events.Delete("fc47dab8b05e7b6d0e549ff3207b9f400adfb8ff96b50ecc4ed51a9ecc75ebbe@group.calendar.google.com", ev.Id).ExecuteAsync(token);
});
Ical.Net.Calendar calendarICS = Ical.Net.Calendar.Load(websiteText);
var eventsICS = calendarICS.Events;
await Parallel.ForEachAsync(eventsICS, parallel, async (ev, token) => {
Event newEvent = new() {
Summary = ev.Summary,
Start = new EventDateTime { DateTime = ev.Start.AsSystemLocal },
End = new EventDateTime { DateTime = ev.End.AsSystemLocal },
Description = ev.Description,
};
await Task.Delay(300, token);
await service.Events.Insert(newEvent, "fc47dab8b05e7b6d0e549ff3207b9f400adfb8ff96b50ecc4ed51a9ecc75ebbe@group.calendar.google.com").ExecuteAsync(token);
});
}
}
}
the part where i'm supposed to be getting the events that are in the "events" var is troubling me: the array is empty. But the adding events part just below is working perfectly. Why? I saw this post: Google Calendar Events Response is Empty and i was even more confused, because my methods has nothing to do with what they have. i tried adding a service account, freshly created, to my agenda: nothing. Making my agenda public: nothing. Whatever i do, this array is empty. Best thing is that it wasn't earlier this morning, and now it is. And i know this might be dumb to specify, but i triplechecked my agenda, and it's not empty.
Worst thing is : deleting used to work.
EDIT: saw this post: Google Calendar v3 API [Events: list] request return Empty List An answer was really interesting... Detailed the way google doesn't really deleted the events or so...? I'm so confused. "There is no way I know of to return all events in one call. You need to loop through the process getting a page at a time until the "NextPageToken" is no longer returned. This makes sense, because for users that have huge calendars with 1000's of appointments, it's inefficient to return everything in one request." Even more here, never heard of nextpagetoken or so