C#从google calendar api

时间:2015-06-20 19:05:57

标签: c# google-calendar-api

我需要一些帮助。可能很容易,但我不明白。 在我们的firestation中是一个显示警报的屏幕(地址,关键字......)。当没有警报时,我想显示即将发生的事件,如生日等。运行屏幕的软件需要" .txt" -files进行输入。  所以我想要做的是:我创建一个谷歌日历并将所有事件放入其中。每天晚上任务调度程序运行一小段软件并从谷歌下载事件,将所有内容写入一个文件,警报软件读取此文件。

到目前为止,我到目前为止:

  • 我创建了一个谷歌日历API。
  • 我从谷歌下载了一个代码示例,并使用了两个日历。

就是这样。现在我整天陷入困境,我想我是一名比程序员更好的消防队员。所以我需要帮助。请给我一些提示如何做。

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
   {
    static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
    static string ApplicationName = "Google Calendar API Quickstart";

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

        using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials");

            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. CALENDAR
        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;
        // END REQUEST

        // Define parameters of request. BIRTHDAY
        EventsResource.ListRequest request1 = service.Events.List("#contacts@group.v.calendar.google.com");
        request1.TimeMin = DateTime.Now;
        request1.ShowDeleted = false;
        request1.SingleEvents = true;
        request1.MaxResults = 10;
        request1.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
        // END REQUEST


        // List events. CALENDAR
        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);
            }
        } // END LISTE

        // List events. BIRTHDAY
        Events events1 = request1.Execute();
        Console.WriteLine("Upcoming events:");
        if (events1.Items != null && events1.Items.Count > 0)
        {
            foreach (var eventItem in events1.Items)
            {
                string when = eventItem.Start.DateTime.ToString();
                if (String.IsNullOrEmpty(when))
                {
                    when = eventItem.Start.Date;
                }
                Console.WriteLine("{0} ({1})", eventItem.Summary, when);
            }
        } // END LISTE

        else
        {
            Console.WriteLine("No upcoming events found.");
        }
        Console.Read();

    }
}
}

1 个答案:

答案 0 :(得分:0)

我建议您创建一个List<String>对象(可能名为ListOfLines,最初为空,然后为您希望文件添加的每一行添加一个字符串。然后,如果列表中已包含所有行(按适当顺序),则只需使用File.WriteAllLines(filename, ListOfStrings),将创建一个具有给定文件名的文件,其行将是列表中的字符串。

从我看到的情况来看,您只需在当前代码上用Console.Writeline(somestring)替换ListOfLines.Add(somestring)。 另外,请注意,您不需要在字符串中明确添加换行符,File.WriteLines会为您执行此操作。