简体   繁体   中英

How to integrate facebook in android and login and get user details and logout

How to integrate facebook in android and login and get user details and logout.

The code JSONObject json = Util.parseJson(mFacebook.request("me")); is giving me null.
I get access token from bundle after OnComplete() .

You should post more code of what you've done, that way it's easier to understand your question. Also, if your receiving an error, post the logcat output. Check this link out. It's the Facebook Developer tutorial for Android which takes you step by step to incorporating Facebook into your app. Now, the tutorial can be vague and hard to understand at times so check this link out. It's a video tutorial that walks you through the steps and is pretty easy to follow. Well, good luck!

This worked out for me try this:

    public class FacebookIntegrationActivity extends AppCompatActivity {

    ActivityFacebookIntegrationBinding binding;
    CallbackManager callbackManager;
    private static final String EMAIL = "email";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil.setContentView(FacebookIntegrationActivity.this, 
    R.layout.activity_facebook_integration);

    //[apply click on view]
    applyClickOnView();
    //method to integrate facebook
    facebookIntegration();

  }

  private void applyClickOnView() {
       binding.btnCustom.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
            LoginManager.getInstance().logOut();

LoginManager.getInstance().logInWithReadPermissions(FacebookIntegrationActivity.this, 
 Arrays.asList("public_profile", "email"));
        }
    });
 }

  //[for facebook integration]
    private void facebookIntegration() {
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code to get user profile
                    requestUserProfile(loginResult);
                }

                @Override
                public void onCancel() {
                    String s = "";
                    //code on cancling of request
                }

                @Override
                public void onError(FacebookException exception) {
                    String s = "";
                    //code on error occurence
                }
            });
     }

   private void requestUserProfile(LoginResult loginResult) {
       final GraphRequest request = 
       GraphRequest.newMeRequest(loginResult.getAccessToken(), new 
       GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject me, GraphResponse response) {
            if (response.getError() != null) {
            } else {
                try {
                    String id = "";
                    String firstName = "";
                    String lastName = "";
                    String email = "";
                    String userPicUrl = "";
                    String userName = "";
                    if (response.getJSONObject().has("id")) {
                        id = response.getJSONObject().get("id").toString();
                    }
                    if (response.getJSONObject().has("first_name")) {
                        firstName = 
                    response.getJSONObject().get("first_name").toString();
                        userName = firstName;
                    }
                    if (response.getJSONObject().has("last_name")) {
                        lastName = 
                    response.getJSONObject().get("last_name").toString();
                        if (!firstName.equals("")) {
                            userName = firstName + " " + lastName;
                        } else {
                            userName = lastName;
                        }
                    }

                    if (response.getJSONObject().has("email")) {
                        email = response.getJSONObject().get("email").toString();
                    }
                    if (response.getJSONObject().has("id")) {
                        userPicUrl = "https://graph.facebook.com/" + id + "/picture? 
                       type=normal";
                    }
                    //send data to api......in live projects 
              //else display on the view........

                    binding.txtName.setText(userName);
                    binding.txtEmail.setText(email);
            //use picasso or glide to display user profile picture in any 
             ImageView                

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id, first_name, last_name, email");
    request.setParameters(parameters);
    request.executeAsync();

   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
   }


  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM