async Task<Account> AuthAsync(OAuthApi api) { Account result = null; try { result = await api.Authenticate(); Toast.MakeText(this, "Successfully Authenticated!", ToastLength.Long).Show(); } catch (Exception ex) { Console.WriteLine(ex); Toast.MakeText(this, "Authentication Faild!", ToastLength.Long).Show(); } if (result != null) Console.WriteLine(await result.ToJsonAsync()); return result; }
public App() { var scopes = new[] { "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile" }; api = new GoogleApi("google", "clientid", "clientsecret",new ModernHttpClient.NativeMessageHandler()) { Scopes = scopes, }; var button = new Button { Text = "Login", }; button.Clicked += async (sender, args) => { try { var account = await api.Authenticate(); Console.WriteLine(account.Identifier); } catch (TaskCanceledException ex) { Console.WriteLine("Canceled"); } }; // The root page of your application MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { button } } }; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); api = new OAuthApi("google",new OAuthAuthenticator( "authUrl", "tokenUrl", "redirecturl", "clientid", "clientsecret")); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById<Button>(Resource.Id.MyButton); button.Text = "Login"; button.Click += async delegate { var account = await api.Authenticate(); Console.WriteLine(account.Identifier); }; }