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

安稳与你 提交于 2019-12-06 06:54:51

问题


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

回答1:


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;
}



回答2:


Am i implementing 4.6.0 FacebookSDK and phasing these error but after manage proper structure of classes got success..!!

AccessToken Class object's initialize after GraphRequest class & inside GraphRequest use object of AccessToken

AccessToken accessToken = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {

                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // Application code
                        System.out.println("object>>" + object);
                        System.out.println("response>>" + response.toString());

                    }
                });


来源:https://stackoverflow.com/questions/21610689/android-app-using-facebook-login-an-active-access-token-must-be-used-to-query-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!