Created
April 12, 2017 23:17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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