Facebook login for Xamarin.Android?

144 views
Skip to first unread message

exosk...@gmail.com

unread,
Oct 25, 2016, 8:22:19 AM10/25/16
to back{4}app
hello guys, has anyone implement something like this?

Davi Macêdo

unread,
Oct 25, 2016, 9:39:51 AM10/25/16
to back{4}app
Hi.

Take a look if this link can help you:

Best!

exosk...@gmail.com

unread,
Oct 25, 2016, 9:54:32 AM10/25/16
to back{4}app
I have seen it but i wasnt able to make it
Message has been deleted

exosk...@gmail.com

unread,
Oct 25, 2016, 1:58:06 PM10/25/16
to back{4}app
This is my implementation but is ve buggy, sometimes crash, and while the login is saving the data the operations are done on the background and the user doesnt show a wait message or something, its like the app close and opens again after the login is complete, how i can make it better?
I also save the image and the Real name of the user in the parse database

void LoginToFacebook()
 {
 var auth = new OAuth2Authenticator(
 clientId: "809804315805408",
 scope: "",
 authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
 );


 auth.AllowCancel = true;


 // If authorization succeeds or is canceled, .Completed will be fired.
 auth.Completed += LoginComplete;


 var intent = auth.GetUI(this);
 StartActivity(intent);
 }
 
 public async void LoginComplete(object sender, AuthenticatorCompletedEventArgs e)
{
string id = "";
string name = "";
JsonValue obj;
if (!e.IsAuthenticated)
{
var builder = new AlertDialog.Builder(this);
builder.SetMessage("Not Authenticated");
builder.SetPositiveButton("Ok", (o, c) => { });
builder.Create().Show();
return;
}
else {


// 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, e.Account);
await request.GetResponseAsync().ContinueWith(t =>
{
var builder = new AlertDialog.Builder(this);
if (t.IsFaulted)
{
builder.SetTitle("Error");
builder.SetMessage(t.Exception.Flatten().InnerException.ToString());
builder.Show();
}
else if (t.IsCanceled)
{
builder.SetTitle("Task Canceled");
builder.Show();
}
else {
obj = JsonValue.Parse(t.Result.GetResponseText());
id = obj["id"];
name = obj["name"];
}

builder.SetPositiveButton("Ok", (o, c) => { });
builder.Create();
}, UIScheduler);

var accessToken = e.Account.Properties["access_token"].ToString();
var expiresIn = Convert.ToDouble(e.Account.Properties["expires_in"]);
var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn);
var user = await ParseFacebookUtils.LogInAsync(id, accessToken, expiryDate);


try
{
user.Add("Name", name);
}
catch (Exception ex)
{
Console.WriteLine("LoginFragment.cs | LoginComplete() :: user.Add (\"Name\",name); :: " + ex.Message);
}

var webClient = new WebClient();
//var httpClient = new HttpClient(new NativeMessageHandler());
var url = new Uri("http://graph.facebook.com/" + id + "/picture?type=small");
application.currentUserImageUrl = url.ToString();
application.currentUserName = name;
byte[] bytes = null;
//bytes = await httpClient.GetByteArrayAsync(url);
bytes = await webClient.DownloadDataTaskAsync(url);
ParseFile saveImageFile = new ParseFile("profileImage.jpg", bytes);
try
{
user.Add("profile_pic", saveImageFile);
}
catch (Exception ex)
{
Console.WriteLine("LoginFragment.cs | LoginComplete() :: user.Add (\"profile_pic\",saveImageFile); :: " + ex.Message);
}

application.currentUser = user;
await user.SaveAsync();
ChangeScreen();
}
}

exosk...@gmail.com

unread,
Nov 3, 2016, 2:42:36 PM11/3/16
to back{4}app
any ideas?

Davi Macêdo

unread,
Nov 4, 2016, 9:46:57 AM11/4/16
to back{4}app
Hi.

Could you please choose a time slot in the link below?

It will be easier debug it together with you.

best!

exosk...@gmail.com

unread,
Nov 18, 2016, 8:34:30 PM11/18/16
to back{4}app

Davi Macêdo

unread,
Nov 20, 2016, 11:05:35 PM11/20/16
to back{4}app
Thank you for sharing the solution. Best!
Reply all
Reply to author
Forward
0 new messages