2

In login page, I get ACCESS_TOKEN. But In Friendlist page, I don't get ACCESS_TOKEN. My code as follow. please check my code.

user log in code

mFacebook.authorize(this, sPermissions, new AuthorizeListener());

and I use AuthorizeListener(). this code

       public class AuthorizeListener implements DialogListener {
      String actoken;
      @Override
      public void onComplete(Bundle values) {
          if (MainFragment.FacebookD) {
              Log.w(MainFragment.LOG_TAG, "::: onComplate :::");
              Log.w(MainFragment.LOG_TAG, "Access token : " + mFacebook.getAccessToken());
              Token = mFacebook.getAccessToken();
            actoken = getAppPreferences(IntroActivity.this, "ACCESS_TOKEN");
            Log.w("GETAPPPREFERENCES",actoken);
          }
      }
   ......

I also write onActivityResult method. this code.

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

      if (requestCode == MainFragment.FACEBOOK_AUTH_CODE) {
          mFacebook.authorizeCallback(requestCode, resultCode, data);
          Log.w("ACTIVITYRESULT","ACTIVITYRESULT");

                     }
  }
   ......
  Intent intent = new Intent(IntroActivity.this, NorinuriMain.class);
            startActivity(intent);

After Log in, intent page(get friendlist)

get friendlist code

 public void setupFacebookfriend(){
    Log.w("FacebookFREIND","START");
    String userName;
    String userSex;

    try {
        Log.w("gogogogo","START");

        final JSONObject json = Util.parseJson(mFacebook.request("me/friends"));
        JSONArray d = json.getJSONArray("data");
        JSONObject o ;
        for(int i = 0 ; i < d.length() ; i++){
            JSONObject post=d.getJSONObject(i);
            o = d.getJSONObject(i);
            list.add(o.getString("data"));
            list.add(post.getString("name"));
        }

    }catch (Exception e) {
        e.printStackTrace();
    }
    facebookListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list));
    facebookListView.setItemsCanFocus(false);
    facebookListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

this logic is OK. But occur error.

com.facebook.android.FacebookError: An active access token must be used to query information about the current user.

why happen??

user2046270
  • 31
  • 1
  • 4

1 Answers1

0

This error comes when Facebook object does not have a valid access token. You have to check session is valid or not while try to get Facebook friends list.

if (mFacebook.isSessionValid()) {
            Bundle parameters = new Bundle();
            parameters.putString("message", msg);
            try {
                String response = mFacebook.request("me/friends", parameters,
                        "GET");
                System.out.println(response);

                JSONObject obj = Util.parseJson(response);

                Log.i("json Response", obj.toString());
                JSONArray array = obj.optJSONArray("data");

                if (array != null) {
                    for (int i = 0; i < array.length(); i++) {
                        String name = array.getJSONObject(i).getString("name");

                        String id = array.getJSONObject(i).getString("id");

                        Log.i(name, id);
                        arrayListFacebookFriend.add(name);
                        arrayListFacebookFriendId.add(id);
                        // String response2 = mFacebook.request(id+"/feed",
                        // parameters,"POST");
                        // System.out.println(response);
                        // response = mFacebook.request(toID+"/feed",
                        // parameters, "POST");
                    }
                    Log.d("arrayList of Friend>>>>", ""
                            + arrayListFacebookFriend);
                    
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (FacebookError e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            mFacebook.authorize(this, sPermissions, new AuthorizeListener());
        }

check for more information on this links:

Android Facebook .. how to get AccessToken

How to provide permission for login in my android application if user has fb account

I hope it will solve your issue.

Community
  • 1
  • 1
Maulik
  • 3,316
  • 20
  • 31