1

Maybe I am simply not getting "it", with "it" being the overall setup needed to make this work.

I have a website that scrapes other sites for sporting events. I want to automatically create Google Calendar events from the results, so I want to give my Web Application Read/Write access on a Calendar in my GMail account.

I have been trying to wrap my head around this for a week now, but I can't get it to work and it is crushing my self-esteem as a developer.

The way I "understand" it is that I need a Google API v3 Service Account, because I don't need an API key for a particular user. Or do I need a Simple API key (instead of oAuth)?

Anyways, I went with the Service Account.

In my HomeController I am trying to GET a Calendar so I know it all works.

    public void Calendar()
    {
        string serviceAccountEmail = "...@developer.gserviceaccount.com";
        var certificate = new X509Certificate2(
            Server.MapPath("~") + @"\App_Data\key.p12",
            "notasecret",
            X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential =
            new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = new[]
                                {
                                    CalendarService.Scope.Calendar
                                },
                        User = "MY-GMAIL-EMAIL" // Is this correct?
                    }
                    .FromCertificate(certificate));

        BaseClientService.Initializer initializer = new BaseClientService.Initializer();
        initializer.HttpClientInitializer = credential;
        initializer.ApplicationName = "CALENDAR NAME"; // Correct?

        var service = new CalendarService(initializer);
        var list = service.CalendarList.List().Execute().Items; // Exception :-(
    }

The error I am getting:

An exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' occurred in Google.Apis.dll but was not handled in user code

Additional information: Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""

So I tried a bunch of things in Google Calendar, like making it public, adding the service account email as a READ/WRITE user.

What do I need to do to authorize my Web Application so it can create events on my behalf?

NaNerd
  • 305
  • 1
  • 4
  • 10
  • This error is mostly likely due to access to the calendar API. Add service account email address to your calendar this might give it access to your calendar. Check this link https://developers.google.com/admin-sdk/directory/v1/guides/delegation#delegate_domain-wide_authority_to_your_service_account . Also, if you don't want to access users calendars instead of service account you can use Oauth – SGC Dec 04 '14 at 17:37

1 Answers1

0

I have done this with the service account in a similar post. I changed a bit of my code and got it working to list my calendars by switching a few things around. I can create events as well. I didn't add a user as you have done in the initializer, and under application name, it is the name of the application in the dev console. Make sure you name your application. Make sure your service account is shared with your account.

I slightly changed the list part of your code to this in mine and got back the my list of calendars.

var list = service.CalendarList.List();
var listex = list.Execute();

Check out my example at Google API Calender v3 Event Insert via Service Account using Asp.Net MVC

Community
  • 1
  • 1
HighlanderGrogg
  • 156
  • 2
  • 8