title: Xamarin Google Calendar Sample
body: <p>I'm trying to get this sample working with Xamarin (Xamarin Forms)</p>
  
  <p><a href="https://developers.google.com/google-apps/calendar/quickstart/dotnet" rel="nofollow noreferrer">https://developers.google.com/google-apps/calendar/quickstart/dotnet</a></p>
  
  <p>I modified the code so that the Xamarin Forms app would load the credentials json from a resource file. I checked that the resource file is getting loaded by stepping to the code. I also modified the DataStore class because Xamarin Forms can't access the path that is specified in the sample.</p>
  
  <pre><code>using Google.Apis.Auth.OAuth2;
  using Google.Apis.Calendar.v3;
  using Google.Apis.Calendar.v3.Data;
  using Google.Apis.Services;
  using Google.Apis.Util.Store;
  using System;
  using System.Collections.Generic;
  using System.Threading;
  using System.Threading.Tasks;
  using Xamarin.Forms;
  using Xamarin.Forms.Xaml;
  
  namespace TestXamarinFormsLibrary.Pages
  {
      [XamlCompilation(XamlCompilationOptions.Compile)]
      public partial class CalendarPage : ContentPage
      {
          public CalendarPage()
          {
              InitializeComponent();
              Go();
          }
  
          static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
          static string ApplicationName = "Google Calendar API .NET Quickstart";
  
          private async void Go()
          {
              UserCredential credential;
  
              var assembly = GetType().Assembly;
              using (var stream = assembly.GetManifestResourceStream("TestXamarinFormsLibrary.client_secret.json"))
              {
                  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                          GoogleClientSecrets.Load(stream).Secrets,
                          Scopes,
                          "user",
                          CancellationToken.None,
                          new DataStore()).Result;
              }
  
              // Create Google Calendar API service.
              var service = new CalendarService(new BaseClientService.Initializer()
              {
                  HttpClientInitializer = credential,
                  ApplicationName = ApplicationName,
              });
  
              // Define parameters of request.
              EventsResource.ListRequest request = service.Events.List("primary");
              request.TimeMin = DateTime.Now;
              request.ShowDeleted = false;
              request.SingleEvents = true;
              request.MaxResults = 10;
              request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
  
              // List events.
              Events events = request.Execute();
              if (events.Items != null &amp;&amp; events.Items.Count &gt; 0)
              {
                  foreach (var eventItem in events.Items)
                  {
                      string when = eventItem.Start.DateTime.ToString();
                      if (String.IsNullOrEmpty(when))
                      {
                          when = eventItem.Start.Date;
                      }
                      await DisplayAlert("ok", $"{eventItem.Summary} ({when})", "ok");
                  }
              }
          }
      }
  
      public class DataStore : IDataStore
      {
          private Dictionary&lt;string, object&gt; Data = new Dictionary&lt;string, object&gt;();
  
          public Task ClearAsync()
          {
              throw new NotImplementedException();
          }
  
          public Task DeleteAsync&lt;T&gt;(string key)
          {
              throw new NotImplementedException();
          }
  
          public async Task&lt;T&gt; GetAsync&lt;T&gt;(string key)
          {
              if (Data.ContainsKey(key))
              {
                  return (T)Data[key];
              }
  
              return default(T);
          }
  
          public async Task StoreAsync&lt;T&gt;(string key, T value)
          {
              Data.Add(key, value);
          }
      }
  }
  </code></pre>
  
  <p>This is the error I get when I run it:</p>
  
  <p><a href="https://i.stack.imgur.com/dmeK4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dmeK4.png" alt="enter image description here"></a></p>
  
  <p>No exception. I get a similar error on Android. My guess is that it is relying on an oAuth library to open up a web browser, to attempt to allow authentication, but Xamarin Forms does not implement this behaviour. I've also tried turning on all the UWP capabilities, but this didn't help.</p>
  
  <p><strong>How can I get the sample running for Xamarin Forms?</strong></p>