Google .Net服务帐户访问日历

时间:2014-11-19 19:25:02

标签: api calendar google-calendar-api google-oauth

我正在更新调用Google日历API的Web服务应用程序,以列出特定日历的日历事件并插入新的日历事件。我正在尝试将其升级到api的第3版。对于身份验证,我使用的是在Google Developers Console(https://console.developers.google.com)中创建的服务帐户凭据。我可以使用以下代码创建CalendarService:

using System;
using Google.Apis.Auth.OAuth2;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Services;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;

...

        string SERVICE_ACCOUNT_EMAIL = 
         "....googleusercontent.com";
        string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"C:\temp\API Project-123456789.p12";

        // Create the service.

        X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
                   new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
                   {
                       Scopes = new[] { CalendarService.Scope.Calendar }
                      , User = "something@mycompany.com"

                   }.FromCertificate(certificate));

        // Create the service.
        var cs = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Calendar API Sample",
        });   

但是当我调用list方法来查询公共日历时:

Events events = service.Events.List(“something@mycompany.com”)。执行();

抛出TokenResponseException,并显示以下错误消息:

错误:“invalid_grant”,描述:“”,Uri:“”

仅供参考:我已进入公司的AdminHome并在安全管理客户API访问,并将上面的SERVICE_ACCOUNT_EMAIL注册到http://www.google.com/calendar/feeds/

对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为你需要注册你的应用程序的服务帐户的客户端ID,我已经使用这种差异成功地使用了它。

这是我通过插入事件解决的类似问题。 Google API Calender v3 Event Insert via Service Account using Asp.Net MVC

以下是有关域范围授权的Google文档。 https://developers.google.com/+/domains/authentication/delegation

相关问题