Google Calendar Api v3 asp.net Code 200,但未插入事件 [英] Google Calendar Api v3 asp.net Code 200, yet no event inserted

查看:19
本文介绍了Google Calendar Api v3 asp.net Code 200,但未插入事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我目前正在 asp.net core 中构建约会查找器.访问日历时,我设法获得了代码 200.

I am currently building an appointment finder in asp.net core. I managed to get a code 200 when accessing the calendar.

但是,我想发送的事件没有被谷歌日历保存.控制台仪表板显示它已收到.帐户中只有一个日历.

However, the event I want to send is not saved by Google Calendar. The console dashboard shows it's received. There is only one calendar in the account.

到目前为止我的代码:

 public void InsertIntoCalendar()
    {
        var Cal = new CalendarEvents();
        var newEvent = Cal.CreateEvent("TestEvent", "RandomLocation", "Description", "2017-07-28");
        var service = CreateService();

        service.Events.Insert(newEvent, serviceAccountEmail).Execute();

            String calendarId = "primary";
            EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
            Event createdEvent = request.Execute();
    }

CalendarEvents 是一个通过多种方法创建事件的类.我尝试使用 Event 类本身,但结果是一样的.

CalendarEvents is a class that's creating an Event by several methods. I tried using the Event class itself, but the outcome is the same.

服务是这样创建的:

        private CalendarService CreateService()
    {
        string keyFilePath = "random.p12;

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

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = Scopes
            }.FromCertificate(certificate));

        // Create the service.
        CalendarService service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            //ApplicationName = "ShowCase",
        });

        return service;
    }

在 cshtml 页面上,我使用 razor 执行此操作:

On the cshtml page, I do this with razor:

            CalendarConnection TestConnection = new CalendarConnection();
            TestConnection.InsertIntoCalendar();     

我收到代码 200,但该活动未插入日历.我阅读了关于这个的 perl 文章,但我无法从中做出任何事情.

I am getting a code 200, but the event is not insterted into the calendar. I read the perl article about this, but I cannot make anything out of it.

我做错了什么?

提前致谢.

当我从控制台应用程序调用 events.list 时,我可以看到事件:

When I call events.list from a console application, I can see the events:

测试 (26.07.2017 20:58:08)

TEST (26.07.2017 20:58:08)

测试 (26.07.2017 20:58:57)

TEST (26.07.2017 20:58:57)

测试 (26.07.2017 21:03:38)

TEST (26.07.2017 21:03:38)

测试 (26.07.2017 21:05:27)

TEST (26.07.2017 21:05:27)

为什么我在日历中看不到它们?

Why don't I see them in the calendar?

推荐答案

我发现了错误.

问题实际上不是我的代码.

The problem wasn't my code actually.

首先,如果您使用primary"作为日历 ID,那么它就不是我帐户的日历 ID.您可以从中获取事件,但它不会显示在您的日历中.一位同事认为这是一种沙盒日历(或错误).

First of all, if you use "primary" as calendar id, it somehow is not the calendar id of my account. You can get events from it, but it's not shown in your calendar. A colleague suggested it's kind of a sandbox calendar (or a bug).

在我输入帐户中的日历 ID(您的主要电子邮件地址或您在日历设置中找到的地址)后,我收到了 403 错误.

After I entered the calendar id from the Account (either the e-mail address for your primary or an address you find in the calendar settings), I got an error 403.

这是由于未与服务帐户地址(Google 在创建服务帐户时创建的地址,通常类似于 XXXXX@xxxxx.iam.gserviceaccount.com)共享日历所致

This was caused by not sharing the calendar with the service account adress (an andress that is created by Google while creating a service account, usually like XXXXX@xxxxx.iam.gserviceaccount.com)

因此,如果您想通过 Google 日历创建活动:

So, if you wanna create events through Google Calendar:

  1. https://console.developers.google.com 创建一个服务帐户

与服务帐户的电子邮件地址共享您的日历.

Share your calendar with the service account's e-mail address.

使用上面的代码(或使用 client_secrets.json 的代码)

Use the code above (or one that uses a client_secrets.json)

注意:它并不总是您的代码,而是缺少一些管理工作.

Note: It is not always your code, but some administration work that is missing.

代码在这里:

 public void InsertIntoCalendar()
{
    var Cal = new CalendarEvents();
    var newEvent = Cal.CreateEvent("TestEvent", "RandomLocation", "Description", "2017-07-28");
    var service = CreateService();

    service.Events.Insert(newEvent, serviceAccountEmail).Execute();

        String calendarId = "XXXXXX@group.calendar.google.com"; // or the e-mail address of the Google account if you want to use the primary calendar
        EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
        Event createdEvent = request.Execute();
}

这篇关于Google Calendar Api v3 asp.net Code 200,但未插入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆