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

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

问题描述


您好,

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

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

但是,我要发送的事件未由Google日历保存。控制台仪表板显示已收到。帐户中只有一个日历。

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页面上,我用剃刀完成此操作:

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:

TEST(26.07.2017 20:58:08)

TEST (26.07.2017 20:58:08)

TEST (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.

这是由于未与服务帐户地址共享日历(创建的Andress)引起的由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代码200,但未插入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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