0

I am attempting to retrieve a list of Google Calendar Events via the .NET v3 Calendar API.

The following code seems correct, but fails when I call request.Execute(); with the error "object reference not set to an instance of an object" shown here. I don't know how to debug the source of the error, or what "object" isn't being set.

        ServiceAccountCredential credentialService;

        CalendarService service;

        var certificate = new X509Certificate2("filename.p12", "notasecret", X509KeyStorageFlags.Exportable);

        credentialService = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer("name@here.iam.gserviceaccount.com")
            {
                Scopes = scopes
            }.FromCertificate(certificate));

        // Create the service.
        service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credentialService,
            ApplicationName = "app name",
        });


        try
        {
            // Define parameters of request.
            EventsResource.ListRequest request = service.Events.List("primary");
            request.TimeMin = DateTime.Now;
            request.ShowDeleted = false;
            request.SingleEvents = true;
            request.MaxResults = 20;
            request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;


            **Events events = request.Execute();**
            if (events.Items != null && events.Items.Count > 0)
            {
                foreach (var eventItem in events.Items)
                {
                    string when = eventItem.Start.DateTime.ToString();
                    if (String.IsNullOrEmpty(when))
                    {
                        when = eventItem.Start.Date;
                    }

                    Item item = new Item();

                    item.Id = Guid.NewGuid().ToString();
                    item.Text = when;
                    item.Description = eventItem.Summary;

                    itemsMeetings.Add(item);

                    //Console.WriteLine("{0} ({1})", eventItem.Summary, when);
                }
            }
            else
            {
                //Console.WriteLine("No upcoming events found.");
            }
        }
        catch(Exception ex)
        {
            Console.Write(ex.Message);
        }
  • Hi, Im just curious, is there a dialog prompt shown when you ran the code? to verify that the credentials are working for the service account? This might also be helpful https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Ron M Jul 01 '21 at 22:12
  • No user prompt is issued. I suppose I could verify the credentials, not sure how I would do that. – user3685350 Jul 02 '21 at 17:14
  • Please try to replace `primary` with your exact calendar id and share your calendar to your service account and see if it will fix the issue. – Ron M Jul 02 '21 at 18:44
  • @Ron M, yes! That did the trick, thank you. – user3685350 Jul 04 '21 at 19:19

1 Answers1

0

The reason why you encountered such error is because the sample code you are using which is based on the official documentation uses a Google account credentials to access its primary calendar. While you aim to use a service account to access/modify your primary calendar, service.Events.List("primary") will return a null since your service account doesn't have a primary calendar.

If you want to update or access your calendar using a service account, You need to share your calendar to your service account to gain access. Share your calendar with someone.

After you gave permission to your service account, you need to provide the exact calendar id is you want to access using your service account instead of primary.

Ron M
  • 5,791
  • 1
  • 4
  • 16
  • @user3685350, I decided to post this as an answer for future reference in the community. Feel free to accept the answer. By doing so, other people in the community, who may have the same concern as you, will know that theirs can be resolved. If the accept button is unavailable to you, feel free to tell me. [How to accept answer](https://meta.stackexchange.com/a/5235) – Ron M Jul 05 '21 at 15:19