如何使用服务帐户在.NET中访问域用户的日历? [英] How to access domain users' calendar using service account in .net?

查看:188
本文介绍了如何使用服务帐户在.NET中访问域用户的日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与key.p12证书服务帐户来访问谷歌日历的API。然而,它不能访问任何用户的日历中的域。我没有按照步骤来委派域范围内的权力,服务帐户 https://developers.google.com/accounts/docs/OAuth2ServiceAccount

I am using service account with key.p12 cert to access google calendar API. However, it cannot access any user's calendar in the domain. I did follow the steps to Delegating domain-wide authority to the service account https://developers.google.com/accounts/docs/OAuth2ServiceAccount

它不具有.NET code样本。而且,似乎我只得到ServiceAccountCredential在谷歌.NET客户端库。这是我的code

It does not have code sample for .net. And it seems I only get ServiceAccountCredential in the google .net client library. Here is my code

    static void Main(string[] args)
    {
        string serviceAccountEmail = "xxxx@developer.gserviceaccount.com";
        var certificate = new X509Certificate2(@"clientPrivateKey.p12", "notasecret",  X509KeyStorageFlags.Exportable);

        Console.WriteLine("service account: {0}", serviceAccountEmail);

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

        BaseClientService.Initializer initializer = new  BaseClientService.Initializer();            

        initializer.HttpClientInitializer = credential;           
        initializer.ApplicationName = "Google Calendar Sample";
        CalendarService calservice = new CalendarService(initializer);

        // list all the calendars it can see
        try
        {
            var list = calservice.CalendarList.List().Execute();

            if (list.Items.Count > 0)
            {
                foreach(var item in list.Items)
                {
                    Console.WriteLine("Found calendar for account {0}", item.Id);
                }
            }
            else
            {
                Console.WriteLine("Calendar list for this service account is empty");
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

日历列表始终是空的。如果我手动在日历设置此服务帐户分享我的域帐户的日历,那么这个code返回我的域帐户的日历成功。

The calendar list is always empty. If I manually share my domain account calendar with this service account in the calendar setting, then this code returns my domain account calendar successfully.

有没有一种方法,使这项服务帐户的所有访问域中的用户的日历?

Is there a way to make this service account access all the user's calendar in the domain?

推荐答案

其实,code应该使用服务帐户冒充域用户一个接一个,而不是试图与服务帐户共享日历。

Actually, the code should use service account to "impersonate" the domain users one by one, rather than trying to share calendars with service account.

    ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = new[] { CalendarService.Scope.Calendar },
        User = "myaccount@mydomain.com" // impersonate domain user
    }.FromCertificate(certificate));

另外需要遵循的步骤委派域范围内的权力,在谷歌域管理员控制台服务帐户,并添加合适的范围内(日历,它的 https://www.googleapis.com/auth/calendar

这篇关于如何使用服务帐户在.NET中访问域用户的日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆