簡體   English   中英

如何使用C#和Google日歷API創建Google日歷活動?

[英]How to create a Google calendar event with C# and Google calendar API?

我有一些用於創建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);          
    }
}

}

這是一個錯誤,我不明白這意味着什么。

在此輸入圖像描述

錯誤消息“請求的身份驗證范圍不足”表示您在令牌中使用的范圍存在問題。 我可以在你的代碼中看到你正在使用正確的范圍來執行這些操作,所以很可能問題是token.json還沒有使用新范圍進行更新,而且它仍然是以前的范圍不允許那些行動。

解:

刪除文件token.json並再次運行腳本,它會提示您授予訪問權限的鏈接,一旦接受,它將再次使用更新的范圍創建token.json。 每次更改范圍時都必須執行此操作。

如果上述方法無法解決問題,另一個重要的事情是啟用您將在Cloud Platform控制台中使用的服務,在本例中為Calendar API(快速入門[1]的第1步)。

請記住,如果您在Cloud Platform控制台中為項目啟用了其他API,則必須創建一個新的credentials.json,它將更新已啟用的服務並替換上一個的credentials.json。

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

暫無
暫無

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

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