Integrate facebook login to get friends in the app

1.facebook login function

1. Create application information in Facebook developer website

The website is here
1. Do it step by step
2. In "generate development key hash", there are two options: Mac operating system and Windows operating system. For general Windows operating system, you need to install "OpenSSL for Windows OpenSSL function library" (download link next to it) Google Code Archive

2.facebook login (you can use the LoginButton provided, or you can not use it)

  1. Registering Callbacks
private CallbackManager callbackManager;
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                //Login successfully
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });

2. invoke callbackManager.onActivityResult in the onActivityResult method and pass the login result to LoginManager through callbackManager.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }
  1. Call login interface
public static void login(Context context) {
        LoginManager.getInstance().logInWithReadPermissions((Activity) context,
                Arrays.asList("public_profile", "user_friends", "email"));
    }

3. Use of atlas API

Detailed usage ☑ click here
1. Access to basic user information (access to HD head portrait)

//picture.type(large) has been looking for a long time. Another / picture is specially used to return the head portrait
new GraphRequest(getAccessToken(), "me?fields=name,id,email,locale,first_name,last_name,gender,timezone,picture.type(large){url}", null, HttpMethod.GET, new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                callback.onCompleted(response.getJSONObject());
            }
}).executeAsync();
  1. Get friends in the user's app (facebook friends sharing the app)
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/friends?fields=id,name,picture{url}", null, HttpMethod.GET, new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
            }
}).executeAsync();
  1. Get all friends in user facebook
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/taggable_friends?fields=id,name,picture{url}", null, HttpMethod.GET, new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
            }
}).executeAsync();

Note: taggable "friends can only be used if you need to apply for facebook permission, and this thing can only be used for the function of circle friends, not for inviting friends to use the app. Otherwise, the audit will fail. Moreover, the id obtained here is not the id obtained during login.

4. Log out and judge whether to log in

//Logout
LoginManager.getInstance().logOut();
//Determine whether to log in
AccessToken.getCurrentAccessToken() == null?false:true;

Keywords: Windows OpenSSL Mac Google

Added by n00854180t on Tue, 14 Apr 2020 22:21:30 +0300