Reputation: 157
I have some code for creating Google Calendar Event, but it does not work. I try to create event in my Google Calendar from C#.
The source code is taken from Google site (https://developers.google.com/calendar/v3/reference/events/insert), but some part of code is from other link(https://developers.google.com/calendar/quickstart/dotnet).
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CalendarQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-dotnet-quickstart.json
static string[] Scopes = { CalendarService.Scope.Calendar };
static string ApplicationName = "Google Calendar API .NET Quickstart";
static void Main(string[] args)
{
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,
});
Event newEvent = new Event()
{
Summary = "Google I/O 2015",
Location = "800 Howard St., San Francisco, CA 94103",
Description = "A chance to hear more about Google's developer products.",
Start = new EventDateTime()
{
DateTime = DateTime.Parse("2019-06-28T09:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
End = new EventDateTime()
{
DateTime = DateTime.Parse("2019-06-28T17:00:00-07:30"),
TimeZone = "America/Los_Angeles",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "[email protected]" },
new EventAttendee() { Email = "[email protected]" },
},
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 24 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();
Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);
}
}
}
This is an error, and I don't understand what it means.
Upvotes: 1
Views: 5135
Reputation: 3340
The error message "request had insufficient authentication scopes" indicates a problem with the scopes you’re using in your token. I can see in your code you’re using the correct scope to execute those actions, so most probably the problem is that the token.json haven’t been updated with the new scope and it’s still with the previous scopes which don’t allow those actions.
Solution:
Delete the file token.json and run the script again, it’ll prompt you the link to grant access and once accepted it’ll create the token.json again with the updated scopes. You have to do this every time you change the scopes.
If the above doesn’t solve the issue, another important thing is to enable the service you’re going to use in the Cloud Platform console , in this case Calendar API (1st step of the quickstart [1]).
Bear in mind that if you enable an additional API for your project in the Cloud Platform console, you would have to create a new credentials.json which will have the enabled services updated and replace that credentials.json for the previous one.
[1] https://developers.google.com/calendar/quickstart/dotnet
Upvotes: 0