2

I have developed one cross platform app using Xamarin forms. I am stuck on one facebook problem. So my question is how to get facebook user's profile info. I am just able to get "Id" and "Name" of user. However to get full details of user (i.e. email, profile picture, gender, birth date etc...)

enter image description here

Here is my code:

 protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        base.OnElementChanged(e);

        // this is a ViewGroup - so should be able to load an AXML file and FindView<>
        var activity = this.Context as Activity;

        var auth = new OAuth2Authenticator (
            clientId: App.Instance.OAuthSettings.ClientId, // your OAuth2 client id
            scope: App.Instance.OAuthSettings.Scope, // The scopes for the particular API you're accessing. The format for this will vary by API.
            authorizeUrl: new Uri (App.Instance.OAuthSettings.AuthorizeUrl), // the auth URL for the service
            redirectUrl: new Uri (App.Instance.OAuthSettings.RedirectUrl)); // the redirect URL for the service
        auth.AllowCancel = true;
        auth.Completed += LoginComplete;

        activity.StartActivity (auth.GetUI(activity));
    }
    public async void LoginComplete(object sender, AuthenticatorCompletedEventArgs e)
    {
        // We presented the UI, so it's up to us to dismiss it.
       // DismissViewController(true, null);

        if (!e.IsAuthenticated)
        {
            Console.WriteLine("Not Authorised");
            return;
        }

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

        // Now that we're logged in, make a OAuth2 request to get the user's id.

        var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, e.Account);
        var response = await request.GetResponseAsync();

        var obj = JObject.Parse(response.GetResponseText());

        var id = obj["id"].ToString().Replace("\"", ""); // Id has extraneous quotation marks

       // var user = await ParseFacebookUtils.LogInAsync(id, accessToken, expiryDate);
    }
Parth Savadiya
  • 1,203
  • 3
  • 18
  • 40

1 Answers1

3

Finally I got solution by adding fields in request URL.

var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture"), null, e.Account);
Parth Savadiya
  • 1,203
  • 3
  • 18
  • 40