How Can I Fetch Name and Email using Facebook SDK

2019-09-10 09:57发布

问题:

I am using Facebook SDK 4 ,I had Do this before sdk 2... but for me some syntax has changed so I had faced some problem... I'm not able to fetch username,email

This is my XML File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:facebook="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

 <com.facebook.login.widget.LoginButton
  android:id="@+id/LoginFbButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="  Login With Facebook" />

</LinearLayout>

This Is My Code For Login with Facebook

private void loginToFacebook() {
Session.openActiveSession(this, true, new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            Log.i(TAG, "Access Token" + session.getAccessToken());
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        try {


                             firstName=user.getFirstName();
                             lastName=user.getLastName();
                             Log.d("Facebook User", "details:" + firstName+""+lastName);
                             }catch(Exception e){

See My Logcat

  com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here    E/ActivityThread(28413): android.app.ServiceConnectionLeaked: Service com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here  

回答1:

You Can Find Details about ur Soln In this link

also refer Facebook Developer notes

Just Do Something Like This

  GraphRequest request = GraphRequest.newMeRequest(
                     loginResult.getAccessToken(),
                     new GraphRequest.GraphJSONObjectCallback() {
                         @Override
                         public void onCompleted(
                                 JSONObject object,
                                 GraphResponse response) {
                             pDialog.dismiss();
                             Log.d("Response",response.getJSONObject().toString());
                             if (response.getError() != null) {
                                 // handle error
                             } else {
                                 String email = object.optString("email");
                                 String fname = object.optString("fname");
                                 String lname = object.optString("lname");
                               Log.d("Email",email);
                               Log.d("fname",fname);
                               Log.d("lname",lname);
                          //     Log.d("Response", response.getInnerJsobject.toString());

                             }
                         }
                     });


回答2:

If i understand well the v4, you first need to get the token, and then address all the queries you want, i personally do:

    mLoginFacebookButton.setFragment(this);
    mLoginFacebookButton.setReadPermissions(Arrays.asList("email"));
    mLoginFacebookButton.registerCallback(mCallbackManager, callback);

With callback being:

FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(final LoginResult loginResult) {

        GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject user, GraphResponse response) {
                email = user.getString("email");
                firstName = user.getString("first_name");
                lastName = user.getString("last_name");
            }
        }).executeAsync();
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException exception) {

    }
};