簡體   English   中英

谷歌日歷事件插入。NETC#

[英]google calendar event insert .NET c#

當我執行以下代碼時:

    public static Event createDashyboardCalendarEvent()
    {
        static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
        static string ApplicationName = "Outlook Google Syncher";

        Event createdEvent = null;

        try
        {
            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/calendar-dotnet-quickstart.json" );
                Console.WriteLine( "credPath:\"{0}\"", credPath );

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load( stream ).Secrets, Scopes, "user",
                    CancellationToken.None, new FileDataStore( credPath, true ) ).Result;
            }
            // Create Google Calendar API service.
            var service = new CalendarService( new BaseClientService.Initializer( )
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            } );
            Event myEvent = new Event
            {
                Summary = "Test appointment",
                Location = "Here",
                Start = new EventDateTime( )
                {
                    DateTime = new DateTime( 2016, 3, 9, 10, 0, 0 ),
                    TimeZone = "Australia/Brisbane"
                },
                End = new EventDateTime( )
                {
                    DateTime = new DateTime( 2016, 3, 9, 10, 30, 0 ),
                    TimeZone = "Australia/Brisbane"
                },
                Recurrence = new String[]
                {
                "RRULE:FREQ=DAILY;COUNT=2"
                }
            };

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

        }
        catch (Exception ex)
        {
            Console.WriteLine( "Exception:{0}", ex );

            Console.WriteLine( "returning NULL" );
            return null;
        }


        Console.WriteLine( "Success. Returning" );
        return createdEvent;
    }
}

我有一個例外:

Exception:The service calendar has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]

   at Google.Apis.Requests.ClientServiceRequest`1.Execute() in .....

什么東西少了? 我可以執行服務service.Events.List,沒有任何問題。

有沒有人可以看到為什么拋出特定異常的地方?

謝謝

原來我使用了錯誤的授權。 我沒有使用服務授權方法。

現在可以使用

暫無
暫無

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

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