Google calendar api CalendarList List return empty element

醉酒当歌 提交于 2019-12-22 05:00:50

问题


EDIT: The original poster asked this for C#, but the same problem occurs regardless of the library used, and its solution is language independent.

Using C# lib,

        string service_account = "myaccount@developer.gserviceaccount.com";

        var certificate = new X509Certificate2(@"pathtomy-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(service_account)
           {
               Scopes = new[] { CalendarService.Scope.Calendar }
           }.FromCertificate(certificate));


        // Create the service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "My project name",

        });

        var calendarList = service.CalendarList.List().Execute();
        IList<CalendarListEntry> items = calendarList.Items;

items is empty. On https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list, when I try the api, I get the good result.

I really don't understand why I don't have any result : seems like if the service_account do not have the same calendar as my gmail account linked to.

Any suggestion ? Thanks.


回答1:


The solution is to share the existing calendar with the service account

<paste-your-account-here>@developer.gserviceaccount.com

(You can find the account in question under the credentials tab in Google Developers Console, it's called 'EMAIL ADDRESS')



来源:https://stackoverflow.com/questions/24062762/google-calendar-api-calendarlist-list-return-empty-element

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