Forum Xamarin.Forms

Using with Facebook login in xamarin form

GraceQiGraceQi Member ✭✭

I Use Xamarin.Auth for login with Facebook Account, Facebook'sProfile photo Resolution is not clear in my App.

private void FacebookClicked(object sender)
{
PublicClass.LocalStorage.ClearAllStorage();
Helper.Constants.AuthSignUp = 1;
Helper.Settings.GeneralUserType = "";
DependencyService.Get().facebook();
}

**Dependency Service **

public class FacebookAuth : BaseViewModel, IFacebook
{
public void facebook()
{
var auth = new OAuth2Authenticator(
clientId: "1106049452932664",
scope: "email",
authorizeUrl: new System.Uri("https://m.facebook.com/dialog/oauth"),
redirectUrl: new System.Uri("https://www.facebook.com/connect/login_success.html"));

        auth.Completed += Auth_Completed;
        var ui = auth.GetUI((Activity)Forms.Context);
        ((Activity)Forms.Context).StartActivity(ui);
    }

    private async void Auth_Completed(object sender, AuthenticatorCompletedEventArgs e)
    {
        if (e.IsAuthenticated)
        {
            UserDialogs.Instance.ShowLoading();
            var request = new OAuth2Request(
                                            "GET",
                                            new System.Uri("https://graph.facebook.com/me?fields=email,name,first_name,last_name,gender,picture"),//https://graph.facebook.com/me?fields=name,picture,cover,birthday //https://graph.facebook.com/me?fields=email,name,first_name,last_name,gender,picture
                                            null,
                                            e.Account);

            var fbResponse = await request.GetResponseAsync();

            //var fbUserAccessToken = CrossFacebookClient.Current.ActiveToken;
            var obj = JObject.Parse(fbResponse.GetResponseText());
            var email = string.Empty;
            try
            {
                email = obj["email"].ToString().Replace("\"", "");
            }
            catch (Exception ex)
            {
                email = "[email protected]";
            }
            var FacebookAccessToken = e.Account.Properties["access_token"];
            var json = fbResponse.GetResponseText();
            var fbUser = JsonConvert.DeserializeObject<FacebookUserModel>(json);
            var name = fbUser.name;
            Helper.Constants.AuthUserEmail = email;
            Helper.Constants.AuthUserFName = fbUser.name;
            Helper.Constants.AuthUserLName = fbUser.last_name;
            Helper.Constants.AuthUserPhone = string.Empty;
            Helper.Constants.AuthUserProfilePic = fbUser.picture.data.url;
            Xamarin.Forms.MessagingCenter.Send<string>(Convert.ToString(Helper.Constants.isAuthLogin), "AuthLogin");
        }
    }
}

Answers

Sign In or Register to comment.