如何在Android的Facebook的整合,登录并获得用户的详细信息和注销

问题描述 投票:0回答:2

如何在Android的Facebook的整合,登录并获得用户的详细信息和注销。

该代码JSONObject json = Util.parseJson(mFacebook.request("me"));是给我空。 我得到OnComplete()后从包访问令牌。

android facebook-login facebook-android-sdk
2个回答
0
投票

你应该张贴的,你做了什么更多的代码,这样它更容易明白你的问题。此外,如果您收到错误,张贴logcat的输出。检查this link out.这是Android版Facebook的开发教程,带您一步步整合的Facebook到您的应用程序。现在,这本手册很含糊,硬,有时明白所以请this link out.这是引导您完成的步骤,并很容易遵循的视频教程。好吧,祝你好运!


0
投票

这个工作我出去试试这个:

    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);
   }


  }
© www.soinside.com 2019 - 2024. All rights reserved.