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

限于喜欢 提交于 2019-11-27 04:54:16

问题


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 = "createdserviceaccountemailaq@developer.gserviceaccount.com";
    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 = "joeshmoemail@email.com",
                Organizer = false,
                Resource = false
            }
        }
    };
    var insertevent = calservice.Events.Insert(calendarEvent, URL);
    var requestedInsert = insertevent.Execute();

回答1:


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 = "example_client_email@gmail.com" 
            }.FromCertificate(certificate));



回答2:


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.



来源:https://stackoverflow.com/questions/27005568/google-api-calender-v3-event-insert-via-service-account-using-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!