Skip to content

Instantly share code, notes, and snippets.

@anaselhajjaji
Created April 12, 2017 23:17
Show Gist options
  • Save anaselhajjaji/22c66960a5061220977cfd7e3bab7a03 to your computer and use it in GitHub Desktop.
Save anaselhajjaji/22c66960a5061220977cfd7e3bab7a03 to your computer and use it in GitHub Desktop.
void LoginToFacebook(bool allowCancel)
{
var auth = new OAuth2Authenticator(
clientId: "395508370820207", // APP ID retrieved from Facebook created Application
scope: "email", // the scopes for the API
authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"), // the auth URL for the service
redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html"));
// To allow cancel of the authentication
auth.AllowCancel = allowCancel;
// If authorization succeeds or is canceled, .Completed will be fired.
auth.Completed += (s, ee) =>
{
if (!ee.IsAuthenticated)
{
// Not Authenticated
var builder = new AlertDialog.Builder(this);
builder.SetMessage("Not Authenticated");
builder.SetPositiveButton("Ok", (o, e) => { });
builder.Create().Show();
return;
}
// Now that we're logged in, make a OAuth2 request to get the user's info.
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, ee.Account);
request.GetResponseAsync().ContinueWith(t =>
{
if (t.IsFaulted)
{
// Problem in authentication
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Error");
builder.SetMessage(t.Exception.Flatten().InnerException.ToString());
builder.SetPositiveButton("Ok", (o, e) => { });
builder.Create().Show();
}
else if (!t.IsCanceled)
{
// Authentication succeeded, show the toast then go the main activity
var obj = JsonValue.Parse(t.Result.GetResponseText());
Toast.MakeText(this, string.Format("Welcome {0}.", obj["name"]), ToastLength.Long).Show();
StartActivity(typeof(RememberListActivity));
}
}, UIScheduler);
};
var intent = auth.GetUI(this);
StartActivity(intent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment