【问题标题】:Google .Net Service Account access to CalendarGoogle .Net 服务帐户访问日历
【发布时间】: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/

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

【问题讨论】:

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


【解决方案1】:

我相信您需要注册您的应用程序服务帐户的客户端 ID,我已经成功地使用了这个差异。

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

这是关于域范围授权的谷歌文档。 https://developers.google.com/+/domains/authentication/delegation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多