Kailas
Kailas

Reputation: 7578

Graph User returning Null, even though the session is created

I have been trying to integrate facebook to my app, but it always returns a Null Graph user.I tried adding the onActivityResult method as a solution told here, but still doesn't seem to help.

public class FacebookRegistartionActivity extends FragmentActivity 
{
    private Session.StatusCallback mStatusCallback = new SessionStatusCallback();
//  private static final int REQUEST_CODE_SIGN_IN = 1;
    Session session;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        fbRegistration();
    }


    public void fbRegistration() {

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        session = Session.getActiveSession();
        Log.i("", "Session before : "+session);
        if (session == null) {

            session = new Session(FacebookRegistartionActivity.this);
            if (session != null) {
                Session.setActiveSession(session);
                if (session != null && !session.isOpened()&& !session.isClosed()) {
                    session.openForRead(new Session.OpenRequest(this).setCallback(mStatusCallback));
                } else {
                }
            }

        } else {
            Log.i("", "Session null ... : "+session);
            // start Facebook Login
            Session.openActiveSession(this, true, mStatusCallback);
        }
    }

The code doesn't even reach the onActivityResult() method.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("", "requestCode : "+requestCode);
        Log.i("", "resultCode : "+resultCode);
        Log.i("", "data : "+data);

        if (resultCode == RESULT_OK) {
            Session.getActiveSession().onActivityResult(this, requestCode,resultCode, data);
        }
    }


    private class SessionStatusCallback implements Session.StatusCallback {


        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            Log.i("", "session in on status call back....."+session);
                    // Here the session is there, not null
            if (session.isOpened()) {

                Log.i("", "session is opened....."+session.isOpened());
                // make request to the /me API
                Request.newMeRequest(session, new Request.GraphUserCallback() {
                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(final GraphUser user, Response response) {
                        Log.i("in SignUp", "USER :: " + user);
//                      Log.i("in SignUp", "response :: " + response);
                        if (user != null) {
                            try{
                                String fqlQuery = "select url from profile_pic where id=me() and width=200 and height=200";
                                Bundle params1 = new Bundle();
                                params1.putString("q", fqlQuery);
                                Session session = Session.getActiveSession();
                                Request request = new Request(session, "/fql", params1,
                                        HttpMethod.GET, new Request.Callback() {
                                            public void onCompleted(Response response) {
                                                try {
                                                    String serverresponse=response.getGraphObject().getInnerJSONObject().toString();
                                                    Log.e("serverresponse", ""+serverresponse);
                                                    JSONObject jsonobj=new JSONObject(serverresponse);
                                                    JSONArray array=jsonobj.getJSONArray("data");
                                                    Log.e("","data object : "+array);
                                                    jsonobj = array.getJSONObject(0);
                                                    String fbUserImageUrl = jsonobj.getString("url");
                                                    Log.e("","image url : "+fbUserImageUrl);

                                                    Logger.show(Log.INFO, "", " AccessToken with publish stream:: " +Session.getActiveSession().getAccessToken());
                                                    String uniqueId = user.getId();
                                                    String userName = user.getName();
                                                    String image = fbUserImageUrl; //"http://graph.facebook.com/"+user.getId()+"/picture?type=large";
                                                    String emailId = user.asMap().get("email").toString();
                                                    Logger.show(Log.INFO,"","uniqueId :: "+ uniqueId);
                                                    Logger.show(Log.INFO,"","userName :: "+ userName);
                                                    Logger.show(Log.INFO,"","image :: "+ image);
                                                    Logger.show(Log.INFO,"","emailId :: "+ emailId);
                                                    }
                                                catch(Exception e)
                                                {
                                                    e.printStackTrace();
                                                }
                                            }
                                });
                                Request.executeBatchAsync(request);
                            }
                            catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                            }
                        }
                        else
                        {
                            Log.i("", "Graph user null");
                                                    // Always the Graph user is returning Null
                        }
                    }
                }).executeAsync();

            }
            else
            {
                Log.i("", "Session not opened still.....");
            }
        }
    }
}

EDIT :

I checked for answers, then realized, I just missed to add the permissions into the Android Manifest file.

Just realized! I Did everything except adding the following permission in Android Manifest file.

 <uses-permission android:name="android.permission.INTERNET" /> 

But, Still wonder how I was able to get the session. Means, it does not require a net connection? Taken in to account that the device I tested had the facebook app installed in it.

Is the session provided by the facebook app, instead? Wished to know how the facebook app works in the login flow in our app, using the facebook sdk.

Any help regarding this is appreciated.

Upvotes: 2

Views: 1663

Answers (1)

Pr38y
Pr38y

Reputation: 1565

As I have mentioned in comment - Yes it is true that when a facebook Session object is created, it attempts to initialize itself from a TokenCachingStrategy. But there must be a Session token cache. As you have told that you were using same app id from some other app, the app was authorized by the user and the session was created and saved in the cache.

Because facebook manages session based on the app id and as the session was already created it fetched it from cache for your new app. But for fetching graph user when you call Request.newMeRequest, it is making an internet connection, since the internet permission was not there in the manifest, your app was not able to connect to facebook server to fetch the same.

Upvotes: 1

Related Questions