【问题标题】:How to create a Google calendar event with C# and Google calendar API?如何使用 C# 和 Google 日历 API 创建 Google 日历活动?
【发布时间】:2019-10-24 05:14:40
【问题描述】:

我有一些用于创建 Google 日历活动的代码,但它不起作用。我尝试使用 C# 在我的 Google 日历中创建活动。

源代码取自 Google 网站 (https://developers.google.com/calendar/v3/reference/events/insert),但部分代码来自其他链接 (https://developers.google.com/calendar/quickstart/dotnet)。

using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CalendarQuickstart
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/calendar-dotnet-quickstart.json
        static string[] Scopes = { CalendarService.Scope.Calendar };
        static string ApplicationName = "Google Calendar API .NET Quickstart";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

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

        Event newEvent = new Event()
        {
            Summary = "Google I/O 2015",
            Location = "800 Howard St., San Francisco, CA 94103",
            Description = "A chance to hear more about Google's developer products.",
            Start = new EventDateTime()
            {
                DateTime = DateTime.Parse("2019-06-28T09:00:00-07:00"),
                TimeZone = "America/Los_Angeles",
            },
            End = new EventDateTime()
            {
                DateTime = DateTime.Parse("2019-06-28T17:00:00-07:30"),
                TimeZone = "America/Los_Angeles",
            },
            Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" },
            Attendees = new EventAttendee[] {
                new EventAttendee() { Email = "lpage@example.com" },
                new EventAttendee() { Email = "sbrin@example.com" },
            },
            Reminders = new Event.RemindersData()
            {
                UseDefault = false,
                Overrides = new EventReminder[] {
                new EventReminder() { Method = "email", Minutes = 24 * 60 },
                new EventReminder() { Method = "sms", Minutes = 10 },
                }
            }
        };

        String calendarId = "primary";
        EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
        Event createdEvent = request.Execute();
        Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);          
    }
}

}

这是一个错误,我不明白这是什么意思。

【问题讨论】:

标签: c# google-calendar-api google-apis-explorer


【解决方案1】:

错误消息“请求的身份验证范围不足”表示您在令牌中使用的范围有问题。我可以在您的代码中看到您正在使用正确的范围来执行这些操作,因此很可能问题是 token.json 尚未使用新范围进行更新,并且仍然使用不允许的先前范围那些动作。

解决方案:

删除文件 token.json 并再次运行脚本,它会提示您授予访问权限的链接,一旦接受,它将使用更新的范围再次创建 token.json。每次更改范围时都必须这样做。

如果上述方法不能解决问题,另一件重要的事情是启用您将在 Cloud Platform 控制台中使用的服务,在本例中为 Calendar API(快速入门的第一步 [1])。

请记住,如果您在 Cloud Platform 控制台中为您的项目启用了额外的 API,则必须创建一个新的 credentials.json 来更新已启用的服务并将该 credentials.json 替换为之前的服务。

[1]https://developers.google.com/calendar/quickstart/dotnet

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2023-03-07
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多