Astgh
Astgh

Reputation: 216

Send a request with Facebook Data to Server in Android

I am new in Android Development, so please, don't consider my question a dumb one. In my application I have authorization with email, Facebook and twitter. Email part is done, and now I am doing the Facebook authorization part using LoginManager. I reached to a point, where tapping on facebook icon, facebook authorization screen appears, user inserts his credentials, Facebook asks about permission to use the data of user. after it the screen closes and user returns to application Login screen. For email login, user's email and password are sent to server. in Facebook case I don't clearly understand what I should send and how should I do that. Here is my code, please someone help or hint - what I have not done correct ?

facebook.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Call private method

                onFacebookLogin();
            }
        });

****

 private void onFacebookLogin()
    {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile", "user_birthday"));

        LoginManager.getInstance().registerCallback(callbackmanager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {

                        System.out.println("Success");

                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id, email, first_name, last_name, gender, user_birthday");

                        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject json, GraphResponse response) {
                                if (response.getError() != null) {
                                    // handle error
                                    System.out.println("ERROR");

                                } else {
                                    System.out.println("Success");


                                    try {

                                        String jsonresult = String.valueOf(json);
                                        System.out.println("JSON Result" + jsonresult);

                                        String email = json.getString("email");
                                        String id = json.getString("id");
                                        String firstname = json.getString("first_name");
                                        String lastname = json.getString("last_name");
                                        String gender = json.getString("gender");
                                        String birthdate = json.getString("user_birthday");


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

                        });
                        request.executeAsync();


                    }

                    @Override
                    public void onCancel() {
                        Log.d("TAG_CANCEL", "On cancel");

                    }

                    @Override
                    public void onError(FacebookException error) {
                        Log.d("TAG_ERROR", error.toString());

                    }
                });
    }

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

        callbackmanager.onActivityResult(requestCode, resultCode, data);

Upvotes: 1

Views: 441

Answers (2)

Hemant Ukey
Hemant Ukey

Reputation: 328

try to put the intent for moving to the next page after facebook login success like below given code

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                //Toast.makeText(LoginActivity.this, "on success", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("login_type", "facebook");
                startActivity(intent);
                finish();
            }
     @Override
    protected void onResume() {
        super.onResume();

        // Logs 'install' and 'app activate' App Events.
        AppEventsLogger.activateApp(this, getResources().getString(R.string.facebook_app_id));
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Logs 'app deactivate' App Event.
        AppEventsLogger.deactivateApp(this, getResources().getString(R.string.facebook_app_id));
    }

Upvotes: 1

Nitin Jain
Nitin Jain

Reputation: 1344

ok I got the question. when your facebook login is success. fetch all information you need for sign In. like fetch the email address. and for password you can set a parameter authByFacebook="true". hope this works for you.

Upvotes: 1

Related Questions