I am trying to connect to all calendars on our G Suite domain through a service account.
I created a new project and enabled the Google Calendar API in the API and services page.
I created a Google service account with:
- A name
- Roles that seem applicable
- Generated json key file
- Enabled G Suite for whole domain
In the Service Account management page I took my ID (AAA@BBB.iam.gserviceaccount.com)
I entered the G Suite security module and went to ManageOauthClients page where I input the ID and selected google calendar API (https://www.googleapis.com/auth/calendar)
It identified my ID correctly and added:
1234567890 Calendar (Read-Write) https://www.googleapis.com/auth/calendar
Where 1234567890 is the ID that Google took from my AAA@BBB.iam.gserviceaccount.com input.
Now I added the nuget package for Google and the Calendar API and wrote the following code in a command line application in C#:
var scopes = new[] { CalendarService.Scope.Calendar };
ServiceAccountCredential credential;
using (Stream stream = new FileStream("ToprServiceAccount-xxxxyyyyy.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes).UnderlyingCredential as ServiceAccountCredential;
}
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
var list = service.CalendarList.Get("planning@mydomain.com").Execute();
I made sure that the calendar I asked for exists. It is the one from the account I used to actually perform all above steps.
But I still am getting the 404 error, meaning most likely that I don't have permission to access (my own) calendar.
Could it be role related? I am unsure what role(s) to pick when creating the service account.
Also when just trying to get the list of all calendars, it is an empty list.