Search code examples
c#asp.netgoogle-calendar-apigoogle-oauthgoogle-api-dotnet-client

execution of request failed https //www.google.com/calendar/feeds/default/allcalendars/full


I am trying the below code but it always gave me the same exception...

using Google calender api v3

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

Google.GData.Calendar.CalendarService calendarService = new Google.GData.Calendar.CalendarService("App name");
        calendarService.setUserCredentials("[email protected]", "password");

        CalendarQuery query = new CalendarQuery();
        query.Uri = new Uri("https://www.google.com/calendar/feeds/default/allcalendars/full");
        CalendarFeed resultFeed = (CalendarFeed)calendarService.Query(query);
        Console.WriteLine("Your calendars:\n");
        foreach (CalendarEntry entry in resultFeed.Entries)
        {
            Console.WriteLine(entry.Title.Text + "\n");
        }

help me!


Solution

  • In order to access Google Calendar you need to be Authenticated with Oauth2, to my knowledge client login is not allowed with Google Calendar API v3.

    Update: From the Google Calendar API documentation

    About authorization protocols

    Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google+ Sign-In, some aspects of authorization are handled for you.

    The following is a simple example on how to create an authenticated calendar service. The code was ripped from the Google Calendar api C# authentication tutorial

     string clientId = "";//From Google Developer console https://console.developers.google.com
     string clientSecret = "";//From Google Developer console https://console.developers.google.com
     string userName = ""//  A string used to identify a user.
     string[] scopes = new string[] {
        CalendarService.Scope.Calendar, // Manage your calendars
        CalendarService.Scope.CalendarReadonly // View your Calendars
     };
    
    // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
            ClientId = clientId, ClientSecret = clientSecret
        }, scopes, userName, CancellationToken.None, new FileDataStore("Daimto.GoogleCalendar.Auth.Store")).Result;
    
    // Create the service.
        CalendarService service = new CalendarService(new BaseClientService.Initializer() {
            HttpClientInitializer = credential,
            ApplicationName = "Calendar API Sample",
        });