HighlanderGrogg
HighlanderGrogg

Reputation: 196

Google API Calender v3 Event Insert via Service Account using Asp.Net MVC

I have been trying to insert a Google calendar event via Google service account that was created for an app in my dev console, but I am continually getting a helpless 404 response back on the Execute method. In the overview of the dev console I can see that the app is getting requests because there are instances of errors on the calendar.events.insert method. There is no information on what is failing. I need this process to use the Service account process instead of OAuth2 so as to not require authentication each time a calendar event needs to be created.

I have set up the service account, given the app a name, have the p12 file referenced in the project. I've also, gone into a personal calendar and have shared with the service account email address. Also, beyond the scope of this ticket, I have created a secondary app, through an administration account and have granted domain wide access to the service account only to receive the same helpless 404 error that this is now giving.

Error Message: Google.Apis.Requests.RequestError
Not Found [404]
Errors [Message[Not Found] Location[ - ] Reason[notFound] Domain[global]

Any help identifying a disconnect or error would be greatly appreciated.

var URL = @"https://www.googleapis.com/calendar/v3/calendars/testcalendarID.com/events";
    string serviceAccountEmail = "[email protected]";
    var path = Path.Combine(HttpRuntime.AppDomainAppPath, "Files/myFile.p12");
    var certificate = new X509Certificate2(path, "notasecret",
    X509KeyStorageFlags.Exportable);

    ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = new[] { Google.Apis.Calendar.v3.CalendarService.Scope.Calendar },
    }.FromCertificate(certificate));
    BaseClientService.Initializer initializer = new
    BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "Test App"
    };
    Google.Apis.Calendar.v3.CalendarService calservice = new Google.Apis.Calendar.v3.CalendarService(initializer);

    string timezone = System.TimeZone.CurrentTimeZone.StandardName;
    var calendarEvent = new Event()
    {
        Reminders = new Event.RemindersData()
        {
            UseDefault = true
        },
        Summary = title,
        Description = description,
        Location = location,
        Start = new EventDateTime()
        {
            //DateTimeRaw = "2014-12-24T10:00:00.000-07:00",
            DateTime = startDateTime,
            TimeZone = "America/Phoenix"
        },
        End = new EventDateTime()
        {
            //DateTimeRaw = "2014-12-24T11:00:00.000-08:00",
            DateTime = endDateTime,
            TimeZone = "America/Phoenix"
        },
        Attendees = new List<EventAttendee>()
        {
            new EventAttendee()
            {
                DisplayName = "Joe Shmo",
                Email = "[email protected]",
                Organizer = false,
                Resource = false
            }
        }
    };
    var insertevent = calservice.Events.Insert(calendarEvent, URL);
    var requestedInsert = insertevent.Execute();

Upvotes: 1

Views: 5156

Answers (2)

Alex Kartynnik
Alex Kartynnik

Reputation: 119

I had the same problem. The solution was to add an email client, whose calendar event you want to send.

 Credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = Scopes,
                User = "[email protected]" 
            }.FromCertificate(certificate));

Upvotes: 2

HighlanderGrogg
HighlanderGrogg

Reputation: 196

So I found out that for this to work, You need to make sure that you access the google.Admin account for referencing the service account Client ID of the app you created.

Another thing that helps is making sure the timezone is in the following format "America/Phoenix"

I have now successfully created events through the service account WITHOUT authentication.

Upvotes: 0

Related Questions