首页 > 解决方案 > 谷歌日历在 ASP .net 中添加事件:身份验证失败

问题描述

在我的 asp.net 核心应用程序中,我将向谷歌日历添加新事件。但它在谷歌显示错误。我已启用日历 api 并插入 ClientId 和 ClientSecret。但它显示错误。

在此处输入图像描述

这是我下面的代码。 在此处输入图像描述在此处输入图像描述

   public void CreateEvent(string email, string text)
    {
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        new ClientSecrets
                        {
                            ClientId = "461480317556-xxxxxxxxxxg.apps.googleusercontent.com",
                            ClientSecret = "RljgIL79D2YFkmVaWQypCjIa",
                        },
                        new[] { CalendarService.Scope.Calendar },"user",CancellationToken.None).Result;

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

        Event myEvent = new Event
        {
            Summary = "Appointment",
            Location = "Somewhere",
            Start = new EventDateTime()
            {
                DateTime = new DateTime(2014, 6, 2, 10, 0, 0),
                TimeZone = "America/Los_Angeles"
            },
            End = new EventDateTime()
            {
                DateTime = new DateTime(2014, 6, 2, 10, 30, 0),
                TimeZone = "America/Los_Angeles"
            },
            Recurrence = new String[] { "RRULE:FREQ=WEEKLY;BYDAY=MO" },Attendees = new List<EventAttendee>(){new EventAttendee() { Email = email } }
        };

        Event recurringEvent = service.Events.Insert(myEvent, "primary").Execute();

    }

标签: asp.net-mvcgoogle-apigoogle-calendar-api

解决方案


我已经解决了我自己的问题。问题是我将类型设置为“Web 应用程序”而不是“其他”。在我将其更改为“其他”类型后它起作用了。

在此处输入图像描述


推荐阅读