Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have this code for getting Calendar information from Goolge Calendar Api v.3. and I have already created Google console account successfully and generated a client id and secret code.

when I run this code, it fails with null values. Exception : System.ObjectDisposedException was unhandled. with this lines of code:
C#
credential =  GoogleWebAuthorizationBroker.AuthorizeAsync(
          new ClientSecrets
          {
              ClientId = "id",
              ClientSecret = "secret"
          },
          new[] { CalendarService.Scope.Calendar },
          "user",
          CancellationToken.None,
          new FileDataStore(@"G:\CalendarCsharp")).Result;




C#
static class Program
   {

       static IList<string> scopes = new List<string>();
       static CalendarService service;
       public static void Main()
       {

           scopes.Add(CalendarService.Scope.Calendar);


           Console.WriteLine("Google.Apis.Calendar.v3");
           Console.WriteLine("==============================");

           UserCredential credential = default(UserCredential);

         credential =  GoogleWebAuthorizationBroker.AuthorizeAsync(
         new ClientSecrets
         {
             ClientId = "id",
             ClientSecret = "secretcode"
         },
         new[] { CalendarService.Scope.Calendar },
         "user",
         CancellationToken.None,
         new FileDataStore(@"G:\CalendarCsharp")).Result;

           BaseClientService.Initializer initializer = new BaseClientService.Initializer();
           initializer.HttpClientInitializer = credential;
           initializer.ApplicationName = "CalendarSample";
           service = new CalendarService(initializer);

           IList<CalendarListEntry> list = service.CalendarList.List().Execute().Items;

           DisplayList(list);
           foreach (Google.Apis.Calendar.v3.Data.CalendarListEntry calendar in list)
           {

               DisplayFirstCalendarEvents(calendar);
           }

           Console.WriteLine("Press any key to continue...");
           Console.ReadKey();
       }

       private static void DisplayList(IList<CalendarListEntry> list)
       {
           Console.WriteLine("Lists of calendars:");
           foreach (CalendarListEntry item in list)
           {
               Console.WriteLine(item.Summary + ". Location: " + item.Location + ", TimeZone: " + item.TimeZone);
           }
       }


       }

   }
Posted
Updated 16-Jul-14 0:14am
v2
Comments
CHill60 16-Jul-14 6:54am    
It seems that several people have had this issue - most have reported that their Account Profile was corrupt - try creating a new Google account and see if that helps.

1 solution

The program failed with two inner exceptions and I had no idea how to catch the inner exception. First I figured out how to catch the inner exceptions using this code :


C#
try
  {
     // your code for getting Calendar values, events etc.
  }
  catch (AggregateException ex)
  {
      foreach (var e in ex.InnerExceptions)
      {
          Console.WriteLine("ERROR: " + e.Message+" "+e.StackTrace);
      }
  }


First Exception : The program was throwing a clumsy Thread Exception. To fix this issue, I had to install a latest version of a package for Microsoft Async 1.0.166 using nuget.

Second Exception : The program was not able to find FileDataStore Method, To fix this I changed my previous code to this one :

C#
UserCredential credential;
           using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
           {

               credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                   GoogleClientSecrets.Load(stream).Secrets,
                   new[] { CalendarService.Scope.Calendar },
                   "user", CancellationToken.None);

           }

           var service = new CalendarService(new BaseClientService.Initializer()
           {
               HttpClientInitializer = credential,
               ApplicationName = "CalendarApp",
           });
await DisplayList(service);

          await credential.RefreshTokenAsync(CancellationToken.None);
          // Request should fail now - invalid grant.
          try
          {
              await DisplayList(service);
          }
          catch (TokenResponseException ex)
          {
              Console.WriteLine(ex.Error);
          }

          // Reauthorize the user. A browser should be opened, and the user should enter his or her credential again.
          await GoogleWebAuthorizationBroker.ReauthorizeAsync(credential, CancellationToken.None);

          // The request should succeed now.
          await DisplayList(service);
       }

       private async Task DisplayList(CalendarService service)
       {
           // Execute async.
           var list = await service.CalendarList.List().ExecuteAsync();
           if(list.Items==null)
           {
               Console.WriteLine("There is no Calendar");
               return;
           }
           Console.WriteLine("Lists of calendars:");
           foreach (CalendarListEntry item in list.Items)
           {
               Console.WriteLine("Item:"+item.Location+"  "+item.Summary);
           // await DisplayFirstCalendarEvents(item, service);
           }
       }

that's all.
 
Share this answer
 
v2
Comments
JBobby 22-Jul-14 5:49am    
sorry, I forgot to add. I used the latest version of client library too. i.e 1.8.2 As we don't need to use FileDataStore method as the user is reauthorized. However, one can implement their own version of FielDatastore method whenever required.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900