簡體   English   中英

如何使用 c# 和 Google API 在 Google 日歷中創建事件?

[英]How to create an event in Google Calendar using c# and Google API?

更新:我解決了這個問題並在下面發布了解決方案作為答案! ;)

我需要創建一個事件並使用 Google API 將其添加到 Google 日歷。

目前我只知道如何從 Google 日歷中獲取我擁有的所有活動。 這是我到目前為止所得到的:

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;


namespace CalendarQuickstart
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/calendar-dotnet-quickstart.json
        static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
        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,
            });

            // Define parameters of request.
            EventsResource.ListRequest request = service.Events.List("primary");
            request.TimeMin = DateTime.Now;
            request.ShowDeleted = false;
            request.SingleEvents = true;
            request.MaxResults = 10;
            request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

            // List events.
            Events events = request.Execute();
            Console.WriteLine("Upcoming events:");
            if (events.Items != null && events.Items.Count > 0)
            {
                foreach (var eventItem in events.Items)
                {
                    string when = eventItem.Start.DateTime.ToString();
                    if (String.IsNullOrEmpty(when))
                    {
                        when = eventItem.Start.Date;
                    }
                    Console.WriteLine("{0} ({1})", eventItem.Summary, when);
                }
            }
            else
            {
                Console.WriteLine("No upcoming events found.");
            }
            Console.Read();
        }
    }
}

我正在嘗試做的必須看起來像這樣:

        var ev = new Event();
        EventDateTime start = new EventDateTime();
        start.DateTime = new DateTime(2019, 3, 11, 10, 0, 0);

        EventDateTime end = new EventDateTime();
        end.DateTime = new DateTime(2019, 3, 11, 10, 30, 0);


        ev.Start = start;
        ev.End = end;
        ev.Description = "Description...";

        events.Items.Insert(0, ev);

我花了一整天的時間搜索任何 .NET 示例,但一無所獲。 任何幫助表示贊賞! ;)

我已經解決了這個問題! 所以在構建項目之前改變

static string[] Scopes = { CalendarService.Scope.CalendarReadonly };

static string[] Scopes = { CalendarService.Scope.Calendar };

如果您已經構建了解決方案,則刪除憑據.json文件,然后重新加載它。 添加事件的代碼在這里:

    var ev = new Event();
    EventDateTime start = new EventDateTime();
    start.DateTime = new DateTime(2019, 3, 11, 10, 0, 0);

    EventDateTime end = new EventDateTime();
    end.DateTime = new DateTime(2019, 3, 11, 10, 30, 0);


    ev.Start = start;
    ev.End = end;
    ev.Summary = "New Event";
    ev.Description = "Description...";

    var calendarId = "primary";
    Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
    Console.WriteLine("Event created: %s\n", e.HtmlLink);

這是我第一次嘗試使用 Google API,所以不要評判我 ;) 希望有一天能對某人有所幫助!

注意:您還需要從 \\bin\\debug 文件夾中刪除“token.json”文件夾

這幾天我嘗試做同樣的事情,我使用了相同的代碼,但就在第一部分(讀取事件)中,系統出現了一個異常“System.InvalidOperationException:'至少一個客戶端機密(已安裝或 Web)應該是set'" 調用方法 credential = GoogleWebAuthorizationBroker.AuthorizeAsync

有人解決了這個問題嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM