I want to create a event in Google Calendar with C#. I am at the first stages in programming but I would like to try to solve this problem.
the code:
private void GoogleAPI_Add_events()
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 8;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
var ev = new Event();
EventDateTime start = new EventDateTime();
start.DateTime = new DateTime(2021, 3, 11, 10, 0, 0);
EventDateTime end = new EventDateTime();
end.DateTime = new DateTime(2021, 3, 15, 10, 0, 0);
ev.Start = start;
ev.End = end;
ev.Summary = "New Event";
ev.Description = "Description...";
var calendarId = "primary";
Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
MessageBox.Show("New evento creato");
}
I get this error:
Google.GoogleApiException: 'Google.Apis.Requests.RequestError Request had insufficient authentication scopes. [403] Errors [
