Register via the Facebook button. Android.

How can I register a user using the Facebook button to read the entered data and send it to the server? Here the user will enter the data on the login page in Facebook. And how can I send a request after

APIService.doSignUp(String username, String password) with the data that the user entered.

enter a description of the image here

Author: Satanist Devilov, 2016-10-01

2 answers

Initialize Facebook SDK and register callback :

public void initFacebookSdk() {
        FacebookSdk.sdkInitialize(activity.getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Log.d("Success", "Login");
                        Log.d(TAG, "Facebook getApplicationId: " + loginResult.getAccessToken().getApplicationId());
                        Log.d(TAG, "Facebook getToken: " + loginResult.getAccessToken().getToken());
                        Log.d(TAG, "Facebook getUserId: " + loginResult.getAccessToken().getUserId());
                        Log.d(TAG, "Facebook getExpires: " + loginResult.getAccessToken().getExpires());
                        Log.d(TAG, "Facebook getLastRefresh: " + loginResult.getAccessToken().getLastRefresh());
                    }

                    @Override
                    public void onCancel() {
                        Toast.makeText(activity, "Login Cancel", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Toast.makeText(activity, exception.getMessage(), Toast.LENGTH_LONG).show();
                        Log.d(TAG, exception.getMessage());
                    }
                });

    }

After that, make an activation call for the login

public void callLoginActivity() {
    LoginManager loginManager = LoginManager.getInstance();
    loginManager.setLoginBehavior(LoginBehavior.NATIVE_WITH_FALLBACK);
    loginManager.logInWithReadPermissions(
            activity,
            Arrays.asList("public_profile", "user_friends", "read_custom_friendlists"));
}

As a result, you will return the data to the onSuccess(LoginResult loginResult) method, after which you will be able to make other requests for user data with the received token.

After you get the token you can request it like this:

/**
 * Get current user access token by Facebook
 *
 * @return - instance of current {@link com.facebook.AccessToken}
 */
public AccessToken getAccessToken() {
    if (AccessToken.getCurrentAccessToken() == null) {
        System.out.println("not logged in yet");
    } else {
        System.out.println("Logged in");
    }
    return AccessToken.getCurrentAccessToken();
}

Next, if you want to get some user profile data from Facebook , you make a request to Graph Api, here , for example, a request that returns data such as name, id,avatar:

/**
 * GraphApi request for get device user info
 * from Facebook as: id, name, avatar
 */
public void getMeInfo() {

    AccessToken token = AccessToken.getCurrentAccessToken();

    GraphRequest request = GraphRequest.newGraphPathRequest(
            token,
            PATH_ME,
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {

                    JSONObject object; //raw response json object
                    JSONObject pictureData; //json object which contains picture data

                    try {

                        //check is response is not empty
                        if (response.getError() == null){

                            //parse json 
                            object = new JSONObject(response.getRawResponse().toString());
                            pictureData = object.getJSONObject("picture").getJSONObject("data");

                            long id = Long.parseLong(object.optString("id");
                            String name = object.getString("name");
                            String url = pictureData.optString("url");
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

    //request params
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,picture.type(large)");
    request.setParameters(parameters);
    request.executeAsync();
}

All other requests to GraphApi are made in the same way. Here is a link to documentation and link Graph Api Explorer - you can protest the query and in it you can generate the code for this request to your application, i.e. you just copy it in the app and will be able to use it, it's very convenient.

 5
Author: Kirill Stoianov, 2016-10-01 12:47:20

Here is the documentation for logging in via facebook.

After authorization, you can get information like: such a user exists, everything is fine, and a token that allows you to work with the facebook api, for example, to read the user's profile.

The password will not be received, the facebook api will only provide you with a login and a token. You can generate a password or use a token instead.

loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
// If using in a fragment
loginButton.setFragment(this);    
// Other app specific specialization

// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
    }

    @Override
    public void onCancel() {
        // App code
    }

    @Override
    public void onError(FacebookException exception) {
        // App code
    }
});
 1
Author: Struv Rim, 2016-10-01 09:29:16