Question

I am trying to use the Google Calendar API in .NET, specifically I am trying to get a list of events. According to the examples here, in different programming languages I need to create a 'service' object and an 'event' object. However, I can't find a clear explanation of what either of these objects is or how to initiate them. Does anyone have an explanation? Or can anyone provide any information or give me a link to where this is explained? It doesn't necessarily have to be in .NET

Here is the example in Java:

String pageToken = null;
do {
   events = service.events().list('primary').setPageToken(pageToken).execute();
List<Event> items = events.getItems();
for (Event event : items) {
  System.out.println(event.getSummary());
}
pageToken = events.getNextPageToken();

} while (pageToken != null); 

Following the advice answered, I am getting the following error:

Could not load file or assembly 'Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Here is the code, the error occurs on the credentials = Await... line

Dim credential As UserCredential
Dim clientSecretsPath As String = Server.MapPath("~/App_Data/client_secret.json")
Dim scopes As IList(Of String) = New List(Of String)()
    scopes.Add(CalendarService.Scope.Calendar)

Using stream = New System.IO.FileStream(clientSecretsPath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, scopes, "user", CancellationToken.None)
    End Using
Was it helpful?

Solution 3

Remember that this sample is for Java. My recommendation is to do the following:

  1. Take a look in our VB sample for the Calendar API which is available here
  2. You should take a look also in other sample for C#, let's say Tasks API sample
  3. Start a new project and add a NuGet reference to Google.Apis.Calednar.v3. Remember that it's prerelease version.
  4. Your code should look like the following:

It's based on the 2 samples above, I didn't compile or test it but it should work.

  UserCredential credential;
  using (var stream = new System.IO.FileStream("client_secrets.json",
  System.IO.FileMode.Open, System.IO.FileAccess.Read))
  {
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets, 
      new[] { CalendarService.Scope.Calendar }, 
      "user", CancellationToken.None);
  }

  // Create the service.
  var service = new CalendarService(new BaseClientService.Initializer()
  {
     HttpClientInitializer = credential,
     ApplicationName = "YOUR APP NAME HERE",
  });     

  var firstCalendar = (await service.CalendarList.List().ExecuteAsync()).Items().FirstOrDefault();
  if (firstCalendar != null)
  {
     // Get all events from the first calendar.
     var calEvents = await service.Events.List(firstCalendar.Id).ExecuteAsync();
     // DO SOMETHING
     var nextPage = calEvents.NextPage;
     while (nextPage != null)
     {
       var listRequest = service.Events.List(firstCalendar.Id);
       // Set the page token for getting the next events.
       listRequest.PageToken = nextPage;
       calEvents = await listRequest.EsecuteAsync();
       // DO SOMETHING
       nextPage = calEvents.NextPage;
     }
  }

OTHER TIPS

The problem with GoogleWebAuthorizationBroker is that it tries to launch a new instance of a web browser to go and get authorization where you have to click the "Grant" button.

Obviously if you're running a MVC project under IIS it's just going to get confused when the code tries to execute a web browser!

My solution:

  1. Download the .net sample projects: https://code.google.com/p/google-api-dotnet-client/source/checkout?repo=samples

  2. Build and run one of the projects relevant to you (Eg Calendar or Drive). Dont forget to include your client_secret.json file downloaded from the cloud console.

  3. Run the project and it will open a new browser on your computer where you will have to click the "Grant" button. Do this once and then your MVC code will work because it will not try to open a web browser to grant the permissions.

I'm not aware of any other way to grant this permission to the SDK but it worked for me just great!

Good luck. This took me a good 5 hours to figure out.

Just had the same issue running VS2013 (using .net45 for my project):

After fetching the CalendarV3 API via NuGet you just have to manually add the reference to:

...packages\Microsoft.Bcl.Async.1.0.165\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll

to the project (because it is not inserted automatically via the NuGet-Script)!

That's it! Maybe @peleyal is correcting the script somewhen in future ;)

I had the same error, and it was due to the app trying to launch the accept screen.
I first tried to get the vb.net example from google and ran that, which I did get to work, and change to my secret info, ran and got the accept screen. I then tried my app, and it still did not work. I noticed that the dll was found here under my project installed from the nuget packages.

...packages\Microsoft.Bcl.Async.1.0.165\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll

but was not in the net45 dir. So I uninstalled the nuget packages (have to if changing the .net version) then changed my .net version for my project to 4.0 instead of 4.5, reinstalled the nuget packages, and then it worked!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top