谷歌API OAuth2,服务帐户;错误";:&引用;无效的“授权”;

谷歌API OAuth2,服务帐户;错误";:&引用;无效的“授权”;,oauth,google-api,google-calendar-api,google-oauth,google-api-dotnet-client,Oauth,Google Api,Google Calendar Api,Google Oauth,Google Api Dotnet Client,我正在尝试使用服务帐户将日历从Dynamics CRM软件同步到Google。 在此期间,我面临着缺乏关于.net的GoogleAPI的文档,特别是关于授权的文档。由于使用了过时的库和类,大多数Google示例甚至无法编译 所以我发现了一些过度实习和接收错误的例子有人能看看我的样品,看看我做错了什么吗? 准备步骤: 我在我的私人谷歌账户中创建了一个项目 在ProjectDeveloper控制台的API&AUTH->Credentials下,我生成了服务帐户。然后单击“生成P12密钥”并下载.P1

我正在尝试使用服务帐户将日历从Dynamics CRM软件同步到Google。 在此期间,我面临着缺乏关于.net的GoogleAPI的文档,特别是关于授权的文档。由于使用了过时的库和类,大多数Google示例甚至无法编译

所以我发现了一些过度实习和接收错误的例子有人能看看我的样品,看看我做错了什么吗?

准备步骤:

  • 我在我的私人谷歌账户中创建了一个项目
  • 在ProjectDeveloper控制台的API&AUTH->Credentials下,我生成了服务帐户。然后单击“生成P12密钥”并下载.P12文件
  • 在API&AUTH->API下,打开“日历API”
  • 然后创建控制台应用程序,并设法安装OAuth和Calendar nuget软件包。有:

  • 谷歌API认证客户端库,Google.api.Auth 1.8.1
  • 谷歌API客户端库,Google.api 1.8.1
  • Google API核心客户端库,Id:Google.api.Core 1.8.1
  • Google.API.Calendar.v3客户端库,Google.API.Calendar.v3 1.8.1.860
  • 找到了一个代码,并根据我的需要进行了调整:

    使用系统;
    使用System.Security.Cryptography.X509证书;
    使用Google.api.Calendar.v3;
    使用Google.api.Auth.OAuth2;
    使用Google.api.Services;
    命名空间crmtogoglecalendar
    {
    班级计划
    {
    静态void Connect()
    {
    var证书=新的X509Certificate2(“我的项目-ee7facaa2bb1.p12”,“notasecret”,X509keystargeFlags.Exportable”);
    var serviceAccountEmail=“50631096175-q2k8hjl141bml57ikufinsh6n8qiu93b@developer.gserviceaccount.com";
    var userAccountEmail=“@gmail.com”;
    var凭证=新ServiceAccountCredential(新ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
    User=userAccountEmail,
    Scopes=new[]{CalendarService.Scope.Calendar}
    }
    .FromCertificate(证书));
    var service=new CalendarService(new BaseClientService.Initializer()
    {
    ApplicationName=“测试日历同步应用程序”,
    HttpClientInitializer=凭证
    });
    var calList=service.CalendarList.List().Execute().Items;
    foreach(calList中的var-cal)
    {
    控制台写入线(校准Id);
    }
    }
    静态void Main(字符串[]参数)
    {
    Connect();
    }
    }
    }
    
    我在应用程序和Fiddler中看到的与Google API的通信是:

    请求:

    主机:HTTPS accounts.google.com,URL:/o/oauth2/token
    断言:长二进制字符串
    授权类型:urn:ietf:params:oauth:grant-type:jwt-bearer

    答复:

    HTTP/1.1 400错误请求内容类型:应用程序/json缓存控制: 无缓存,无存储,最大年龄=0,必须重新验证Pragma:无缓存 到期日期:1990年1月1日星期五00:00:00格林尼治标准时间日期:2014年7月24日星期四06:12:18 GMT X-Content-Type-Options:nosniff X-Frame-Options:SAMEORIGIN X-XSS-Protection:1;模式=块服务器:GSE备用协议: 443:quic传输编码:分块

    1f{“错误”:“无效的授权”}0


    请提前帮助和感谢

    我不确定你的代码到底出了什么问题,我想这就是你加载密钥文件的方式。这也可能是因为您不需要使用ServiceAccountCredential发送User,因为服务帐户就是您登录的用户。我不知道你为什么给别人发Gmail邮件

    using Google.Apis.Auth.OAuth2;
    using System.Security.Cryptography.X509Certificates;
    using Google.Apis.Calendar.v3;
    namespace GoogleAnalytics.Service.Account
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Install-Package Google.Apis.Calendar.v3
                string serviceAccountEmail = "1046123799103-6v9cj8jbub068jgmss54m9gkuk4q2qu8@developer.gserviceaccount.com";
                var certificate = new X509Certificate2(@"C:\Users\HP_User\Documents\GitHub\Google-Analytics-dotnet-ServiceAccount\GoogleAnalytics.Service.Account\Diamto Test Everything Project-bc63fd995bd7.p12", "notasecret", X509KeyStorageFlags.Exportable);
    
                ServiceAccountCredential credential = new ServiceAccountCredential(
                       new ServiceAccountCredential.Initializer(serviceAccountEmail)
                       {
                           Scopes = new[] { CalendarService.Scope.Calendar }
                       }.FromCertificate(certificate));
    
                // Create the service.
                var service = new CalendarService(new CalendarService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "CalendarService API Sample",
                });
    
                // define the new Calendar
                Google.Apis.Calendar.v3.Data.Calendar calendar = new Google.Apis.Calendar.v3.Data.Calendar();
                calendar.Description = "New Calendar";
                calendar.Summary = "New Calendar Summary";
                // Insert the Calendar
                service.Calendars.Insert(calendar).Execute();
                // List The Calendar
                var calList = service.CalendarList.List().Execute().Items;
    
            }
        }
    }
    

    这段代码经过测试,它向您展示了如何为服务帐户创建初始日历。如果不创建日历,它将返回0个日历,您需要记住先创建一个。

    经过一些调查,我发现Google API在您的个人帐户@gmail.com上无法正常工作。你应该有组织域帐户在谷歌的格式you@your_organisation_domain

    然后,还有一点也很让人困惑,在日历API页面中没有提到“将域范围的权限委托给您的服务帐户”部分的文档。 本节共有7个步骤,需要完成

    顺便说一句,个人账户管理网站admin.google.com甚至不可用。因此,使用@gmail.com帐户执行这7个步骤是不可能的

    然后,当客户端在Google Apps管理控制台>安全>高级设置>管理OAuth客户端访问中获得授权时,代码开始工作

    有一个代码适合我:

    使用系统;
    使用System.Collections.Generic;
    使用System.IO;
    使用System.Linq;
    使用System.Security.Cryptography.X509证书;
    使用Google.api.Calendar.v3;
    使用Google.api.Calendar.v3.Data;
    使用Google.api.Auth.OAuth2;
    使用Google.api.Services;
    命名空间crmtogoglecalendar
    {
    班级计划
    {
    静态void Connect()
    {
    Console.WriteLine(“通过OAuth2服务帐户示例的日历”);
    var证书=新的X509Certificate2(“我的MC项目-3f38defdf4e4.p12”,“notasecret”,X509keystrageFlags.Exportable”);
    var serviceAccountEmail=“795039984093-c6ab1mknpediih2eo9cb70mc9jpu9h03@developer.gserviceaccount.com";
    var userAccountEmail=”me@testdomain.com"; 
    var凭证=新ServiceAccountCredential(新ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
    User=userAccountEmail,
    Scopes=new[]{CalendarService.Scope.Calendar}
    }
    .FromCertificate(证书));
    var service=new CalendarService(new BaseClientService.Initializer()
    {
    ApplicationName=“Tes
    
    Calendar via OAuth2 Service Account Sample
    Created event Id: jkits4dnpq6oflf99mfqf1kdo0
    calendar id=me@testdomain.com
    Event ID: 1logvocs77jierahutgv962sus, ICalUID: 1logvocs77jierahutgv962sus@google.com
      Name: test event
      Description: test description2
      Status: confirmed
      Color: 
      Attendees: 
      Kind: calendar#event
      Location: location2
      Organizer: me@testdomain.com
      Recurrence: RRULE:FREQ=WEEKLY;BYDAY=TH
      Start: 2014-07-31
      End: 2014-08-01
      Reminders: Not defailt, email:10,popup:10
    =====================
    Event ID: 1logvocs77jierahutgv962sus_20140814, ICalUID: 1logvocs77jierahutgv962sus@google.com
      Name: test event updated
      Description: test description2
      Status: confirmed
      Color: 
      Attendees: 
      Kind: calendar#event
      Location: location2
      Organizer: me@testdomain.com
      Recurrence: no recurrence
      Start: 2014-08-14
      End: 2014-08-15
      Reminders: Not defailt, email:10
    =====================
    Event ID: 974hqdhh8jhv5sdobkggmdvvd8, ICalUID: 974hqdhh8jhv5sdobkggmdvvd8@google.com
      Name: One hour event
      Description: test description
      Status: confirmed
      Color: 7
      Attendees: 
      Kind: calendar#event
      Location: Meeting Room Hire, Broadway, 255 The Bdwy, Broadway, NSW 2007, Australia
      Organizer: me@testdomain.com
      Recurrence: no recurrence
      Start: 1/08/2014 10:00:00 AM
      End: 1/08/2014 11:00:00 AM
      Reminders: Default
    =====================
    Event ID: jkits4dnpq6oflf99mfqf1kdo0, ICalUID: jkits4dnpq6oflf99mfqf1kdo0@google.com
      Name: Calendar event via API
      Description: Programmatically created
      Status: confirmed
      Color: 6
      Attendees: 
      Kind: calendar#event
      Location: 
      Organizer: me@testdomain.com
      Recurrence: no recurrence
      Start: 2/08/2014 12:30:50 PM
      End: 2/08/2014 1:30:50 PM
      Reminders: Not defailt, popup:60
    =====================