I'm trying to get this sample working with Xamarin (Xamarin Forms)
https://developers.google.com/google-apps/calendar/quickstart/dotnet
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.
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 && events.Items.Count > 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<string, object> Data = new Dictionary<string, object>();
public Task ClearAsync()
{
throw new NotImplementedException();
}
public Task DeleteAsync<T>(string key)
{
throw new NotImplementedException();
}
public async Task<T> GetAsync<T>(string key)
{
if (Data.ContainsKey(key))
{
return (T)Data[key];
}
return default(T);
}
public async Task StoreAsync<T>(string key, T value)
{
Data.Add(key, value);
}
}
}
This is the error I get when I run it:
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.
How can I get the sample running for Xamarin Forms?