【发布时间】:2014-11-26 06:08:54
【问题描述】:
我正在更新一个 Web 服务应用程序,该应用程序调用 Google 的日历 API 来列出特定日历的日历事件并插入新的日历事件。我正在尝试将其升级到 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 = service.Events.List("something@mycompany.com").Execute();
抛出 TokenResponseException 并显示以下错误消息:
错误:“invalid_grant”,描述:“”,Uri:“”
仅供参考:我已进入我公司的 AdminHome,并在安全管理客户端 API 访问权限下,并将上面的 SERVICE_ACCOUNT_EMAIL 注册到http://www.google.com/calendar/feeds/。
对此的任何帮助将不胜感激。
【问题讨论】:
-
google.com/calendar/feeds 是旧范围,您需要包含 googleapis.com/auth/calendar 代替。有一个关于服务帐户委托的 Drive 文档,可以直接映射到日历中:developers.google.com/drive/web/delegation
标签: api calendar google-calendar-api google-oauth