简体   繁体   English

谷歌日历事件插入。NETC#

[英]google calendar event insert .NET c#

When I execute the following code: 当我执行以下代码时:

    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;
    }
}

I get an exception: 我有一个例外:

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 .....

What is missing? 什么东西少了? I can execute the service service.Events.List without any problems. 我可以执行服务service.Events.List,没有任何问题。

Is there a place one can see why a particular exception was thrown? 有没有人可以看到为什么抛出特定异常的地方?

Thank you 谢谢

It turns out I used the wrong authorization. 原来我使用了错误的授权。 I didn't use the service authorization method. 我没有使用服务授权方法。

It works now 现在可以使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM