0

我使用 ServiceAccountCredential 从 Calendar EventsResource.ListRequest 中得到零结果。并且相同的代码适用于 UserCredential。我没有收到任何异常或错误,只是当我尝试使用 key.p12 文件进行身份验证时,Events.Items 计数始终为零。

我还在域管理员安全设置中为此服务帐户的 clientID设置了https://www.googleapis.com/auth/calendar的 API 客户端访问权限。

有任何想法吗?谢谢

--------------------------------这是我的代码:

    static UserCredential getUserCredential()
    {
        UserCredential credential;
        using (var stream = new FileStream(@"client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { CalendarService.Scope.CalendarReadonly },
                "user", CancellationToken.None, new FileDataStore("carol test")).Result;
        }
        return credential;
    }

    static ServiceAccountCredential getServiceAccountCredential()
    {
        //Google authentication using OAuth for single calendar
        string serviceAccountEmail = "xx...@developer.gserviceaccount.com";            
        var certificate = new X509Certificate2(@"clientPrivateKey.p12", "notasecret",
        X509KeyStorageFlags.Exportable);

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

        return credential;
    }

    static void Main(string[] args)
    {
        // Create the service.

        BaseClientService.Initializer initializer = new
        BaseClientService.Initializer();
        //initializer.HttpClientInitializer = getUserCredential();
        initializer.HttpClientInitializer = getServiceAccountCredential();
        initializer.ApplicationName = "Google Calendar Sample";
        CalendarService calservice = new CalendarService(initializer);


     List<CalendarEvent> calendarEvents = new List<CalendarEvent>();
        try
        {
            EventsResource.ListRequest req = calservice.Events.List("primary");    // Is it correct to use "primary" as calendarID?
            req.TimeMin = new DateTime(2014, 12, 1);
            req.TimeMax = new DateTime(2014, 12, 6);
            req.SingleEvents = true;
            req.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
            Events events = req.Execute();  // always empty for ServiceAccount auth, but getting results for user credential auth

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);  // didnot get exception either way
        }

    }

事件的价值(使用 UserCredential 时)

{Google.Apis.Calendar.v3.Data.Events}
AccessRole: "owner"
DefaultReminders: Count = 1
Description: null
ETag: "\"1417813280083000\""
Items: Count = 2
Kind: "calendar#events"
NextPageToken: null
NextSyncToken: null
Summary: "myemail@mydomain.net"
TimeZone: "America/Los_Angeles"
Updated: {12/5/2014 1:01:20 PM}
UpdatedRaw: "2014-12-05T21:01:20.083Z"

事件值(使用服务帐户 key.p12 身份验证时)

{Google.Apis.Calendar.v3.Data.Events}
AccessRole: "owner"
DefaultReminders: Count = 0
Description: null
ETag: "\"1417747728066000\""
Items: Count = 0
Kind: "calendar#events"
NextPageToken: null
NextSyncToken: null
Summary: "xxxx@developer.gserviceaccount.com"
TimeZone: "UTC"
Updated: {12/4/2014 6:48:48 PM}
UpdatedRaw: "2014-12-05T02:48:48.066Z"
4

0 回答 0