googleapis / google-api-dotnet-client

Google APIs Client Library for .NET
https://developers.google.com/api-client-library/dotnet
Apache License 2.0
1.36k stars 525 forks source link

No result from default calendar request, in service mode #1328

Closed evan-boissonnot closed 5 years ago

evan-boissonnot commented 5 years ago

Hi

I try to get my events from service account authentication. So I use this code :

List list = new List();

        using (var fs = new FileStream("google-api-token.json", FileMode.Open, FileAccess.Read))
        {
            var credentialParameters =
                NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(fs);

            var credential = new ServiceAccountCredential(
                                new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
                                {
                                    Scopes = _SCOPES
                                }.FromPrivateKey(credentialParameters.PrivateKey));

            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "BookMyService"
            });

            EventsResource.ListRequest request = service.Events.List("primary");
            //request.TimeMin = DateTime.Now;
            request.ShowDeleted = false;
            request.SingleEvents = true;
            request.MaxResults = 10;
            request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

            // List events.
            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;
                    }

                }
            }
            else
            {
            }
        }

And the .json file comes from google console service account generator.

Please Help !

evan-boissonnot commented 5 years ago

Here my SCOPES : private static string[] _SCOPES = { CalendarService.Scope.CalendarReadonly };

evan-boissonnot commented 5 years ago

WIth this whole code, I got no results int events.Items.

evan-boissonnot commented 5 years ago

I found the solution 👍 I had to share my google calendar with the email of the servcie account !