Ad

Login With Facebook In Android App Does Not Call OnCompleted() Method?

I'm basically implementing Login with Facebook in android app. I go through the steps given at developers.facebook.com and did all of those things that are given there. But in my code onCompleted() method is not called so how can I retrieve user information after successful login. Any help will be appreciated!

Below is my java code

package com.example.rohitsuthar.facebook_signin;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import org.json.JSONObject;

import java.security.MessageDigest;

public class MainActivity extends AppCompatActivity {
    CallbackManager callbackManager;
    AccessToken mAccessTokem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        Log.d("ROHIT","inside on success");
                        mAccessTokem=loginResult.getAccessToken();
                        Log.d("ROHIT","token retrived");
                        GraphRequest graphRequest=GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                //hi
                                Log.d("ROHIT","oncompleted is called");
                            }
                        });

                    }

                    @Override
                    public void onCancel() {
                        // App code
                        Log.e("ROHIT","inside onCancel");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                        Log.e("ROHIT",exception.toString());
                    }
                });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Here is my Log

06-13 15:00:41.238 32721-32721/com.example.rohitsuthar.facebook_signin D/ROHIT: inside on success
token retrived
06-13 15:00:41.239 32721-32721/com.example.rohitsuthar.facebook_signin D/com.facebook.FacebookSdk: getGraphApiVersion: v3.0
06-13 15:00:41.244 32721-32721/com.example.rohitsuthar.facebook_signin D/AccessTokenTracker: AccessTokenChanged

After token is retrieved nothing happens anyone please tell me whats going on and how to fix this!

Ad

Answer

you have to call graphRequest.executeAsync(); inside onSuccess() method after you have defined your GraphRequest object, your code should looks like:

LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        Log.d("ROHIT","inside on success");
                        mAccessTokem=loginResult.getAccessToken();
                        Log.d("ROHIT","token retrived");
                        GraphRequest graphRequest=GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                //hi
                                Log.d("ROHIT","oncompleted is called");
                            }
                        });
                   Bundle parameters = new Bundle();
                   parameters.putString("fields", "id, first_name, last_name, email"); // Here you add the parameters you need from user facebook account
                   graphRequest.setParameters(parameters);
                   graphRequest.executeAsync();
                    }

                    @Override
                    public void onCancel() {
                        // App code
                        Log.e("ROHIT","inside onCancel");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                        Log.e("ROHIT",exception.toString());
                    }
                });
Ad
source: stackoverflow.com
Ad