Search code examples
androidfacebookfacebook-graph-apifacebook-android-sdk

Android app using Facebook login: An active access token must be used to query information about the current user


I have integrated Facebook login in my app, but when trying to get the name out of the basic info of the logged in user, I get:

{
  Response:  responseCode: 400, 
  graphObject: null, 
  error: {
  HttpStatus: 400, 
  errorCode: 2500, 
  errorType: OAuthException, 
  errorMessage: An active access token must be used to query information about the current user.
    }, 
isFromCache:false
}

I logged my session token and its expiration date and it is not null nor expired. I tried replacing session with getActiveSession() but that didn't fix anything.

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {

    final SharedPreferences prefs = getActivity().getSharedPreferences(
            "fbInfo", Context.MODE_PRIVATE);
    if (session != null && session.getState().isOpened()){
        Log.i("sessionToken", session.getAccessToken());
        Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());
    }

    new Request(session, "/me?fields=name", null, HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                            .getGraphObject()
                            .getInnerJSONObject();
                    try {
                        prefs.edit().putString("name", graphResponse.getString("name")).commit();

                    } catch (JSONException e) {
                        Log.i(TAG,
                                "JSON error " + e.getMessage());
                    }
                }
            }
    ).executeAsync();

    new Request(session, "/me?fields=quotes", null, HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                            .getGraphObject()
                            .getInnerJSONObject();
                    try {
                        prefs.edit().putString("quotes", graphResponse.getString("quotes")).commit();

                    } catch (JSONException e) {
                        Log.i(TAG,
                                "JSON error " + e.getMessage());
                    }
                }
            }
    ).executeAsync();}

Solution

  • Try this

    if (state.isOpened())
    {
        Log.i("Facebook", "Logged in...");
    
        new Request(session, "me/photos",getRequestParameters(), null, new Callback()
        {           
            @Override
            public void onCompleted(Response response)
            {
    
    
            }
        }).executeAsync();
    
    ....
    
    
    private Bundle getRequestParameters() 
    {
           Bundle parameters = new Bundle(1);
           parameters.putString("fields", "images,other");
           return parameters;
    }