How can I register a user using the Facebook button to read the entered data and send it to the server. Here the user enters the data on the login page on Facebook. And after you can send a request

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

enter image description here

    2 answers 2

    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 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 can already 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(); } 

    Further, if you want to get some user profile data from Facebook, you make a request to Graph Api , for example, a request that will return data such as your 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 done in a similar way. Here is a link to the documentation and a link to the Graph Api Explorer , in it you can test requests and in it you can generate the code of this request for your application, i.e. you simply copy it into your application and be able to use it, it is very convenient.

    • and how can something not be done on one page? If so, how? I would like to do everything so that the user clicks the "Login via Facebook" button and he enters and after that, make his request with a username and some random password. After this request, it will automatically transfer to full access by the application. - Satanist Devilov
    • Can you reformulate the question, I do not fully understand you? - Kirill Stoianov
    • as it is possible, to display all the code on one page, otherwise I'm new to this business. And how to pull out the usename, email. Could you describe in detail if it's not difficult for you? - Satanist Devilov
    • @SatanistDevilov updated the answer - Kirill Stoianov
    • Thank you so much) - Satanist Devilov

    Here is the authorization documentation via facebook.

    After logging in, you can get information of the form: such a user exists, everything is in order and a token that allows you to work with facebook api, for example, read the user profile.

    The password will not be received, facebook api will provide you only a login and token. Password can be generated 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 } }); 
    • and how correctly to take login and a token though. In the documentation I can not find. ( - Satanist Devilov
    • @Override public void onSuccess(LoginResult loginResult) { // App code } - A token is stored in loginResult, etc. - Struv Rim