Google Calendar API v3 C#无效

时间:2013-05-05 19:17:50

标签: google-api

我正在尝试使用Google Calendar API V3来获取我的日历,使用C#with Service帐户会出错

  

发送直接消息或收到回复时出错。

以下是我的代码。

    private const string SERVICE_ACCOUNT_EMAIL = "XXX@developer.gserviceaccount.com";
    private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"D:\privatekey.p12";
    static void Main(string[] args)
    {
        Test();
    }

    static void Test()
    {
        X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        AssertionFlowClient provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
        {
            ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
            Scope = "Calendar",
        };
        var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

        CalendarService c1 =new CalendarService(
            new BaseClientService.Initializer()
                        {
                            Authenticator = auth
                        });

        c1.Calendars.Get("MyCalenderID").Fetch();

    }

1 个答案:

答案 0 :(得分:-1)

你的范围应该是:

"https://www.googleapis.com/auth/" + 
Google.Apis.Calendar.v3.CalendarService.Scopes.CalendarReadonly.ToString().ToLower()