在Google日历中添加提醒条目

时间:2018-03-05 14:09:11

标签: c# google-api google-oauth google-api-dotnet-client google-calendar-api

我想在Google Calendar中添加提醒。为此,我指的是这篇文章

https://developers.google.com/google-apps/calendar/quickstart/dotnet

但是我的调试器一到达

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
什么都没有。应用程序暂停,控制台上没有打印。

完整代码:我已将其转换为vb代码,因为我需要在VB.Net app中使用它

Sub Main()
        Dim Scopes As String() = {CalendarService.Scope.CalendarReadonly}
        Dim ApplicationName As String = "Google Calendar API .NET Quickstart"
        Dim credential As UserCredential
        Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
            Dim clientSecrets = GoogleClientSecrets.Load(stream).Secrets
            Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
            credPath = Path.Combine(credPath, "D:/calendar-dotnet-quickstart.json")
            Dim dataStore = New FileDataStore(credPath, True)
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, Scopes, "user", CancellationToken.None, dataStore).Result
            Console.WriteLine("Credential file saved to: " & credPath)
        End Using

        Dim service = New CalendarService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = ApplicationName})
        Dim request As EventsResource.ListRequest = service.Events.List("primary")
        request.TimeMin = DateTime.Now
        request.ShowDeleted = False
        request.SingleEvents = True
        request.MaxResults = 10
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime
        Dim events As Events = request.Execute()
        Console.WriteLine("Upcoming events:")
        If events.Items IsNot Nothing AndAlso events.Items.Count > 0 Then
            For Each eventItem In events.Items
                Dim [when] As String = eventItem.Start.DateTime.ToString()
                If String.IsNullOrEmpty([when]) Then
                    [when] = eventItem.Start.Date
                End If

                Console.WriteLine("{0} ({1})", eventItem.Summary, [when])
            Next
        Else
            Console.WriteLine("No upcoming events found.")
        End If

        Console.Read()
    End Sub

2 个答案:

答案 0 :(得分:0)

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;

只是将您的应用程序授权给Google。第一次运行它应该会提示您有权访问您请求的范围的帐户。您可以在credPath中找到包含凭据的文件。

您可能想要添加Console.Readline();这样你的控制台应用程序就会停止,你可以看到它们是什么。

我在这里有一些可能有用的示例代码Google calendar .net

答案 1 :(得分:0)

我将默认浏览器从Firefox更改为Google Chrome并且可以正常运行。可能是我应该向Firefox报告此错误。