简体   繁体   中英

Android Facebook Graph api basic information requesting

just trying to allow my app to access basic information from an authenticated Facebook user, yet my logcat tells me

06-30 16:37:27.969: WARN/System.err(1559): com.facebook.android.FacebookError: An active access token must be used to query information about the current user.

right after authentification where the code is ran. Can someone point me in the right direction? I've seen a very similar post where someone used almost identical code and It worked.

Thanks

Facebook facebook = new Facebook("187212574660004");

TextView nText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nText = (TextView) this.findViewById(R.id.nameText);

    facebook.authorize(this, new String[] {"offline_access", "user_interests", "friends_interests"},

            new DialogListener() {
        public void onComplete(Bundle values) {
            setContentView(R.layout.homepage);
        }

        public void onFacebookError(FacebookError error) {}

        public void onError(DialogError e) {}

        public void onCancel() {}
    });

    JSONObject json_data = null;

    try
    {
        JSONObject response = Util.parseJson(facebook.request("me/friends")); // Get a friend information from facebook
        JSONArray jArray = response.getJSONArray("data");

        json_data = jArray.getJSONObject(0);
        String name = json_data.getString("name");
        Log.i("friend is", name);
        nText.setText(name);

    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (FacebookError e)
    {
        e.printStackTrace();
    }
}

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

    facebook.authorizeCallback(requestCode, resultCode, data);
}

Tried the method as described before by Manno23 and got

06-30 19:27:04.338: ERROR/AndroidRuntime(349): Caused by: java.lang.NullPointerException 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage.getFriends(FacebookPage.java:67) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage.access$0(FacebookPage.java:55) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage$1.onComplete(FacebookPage.java:41) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.facebook.android.Facebook.authorizeCallback(Facebook.java:383) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage.onActivityResult(FacebookPage.java:93) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at android.app.Activity.dispatchActivityResult(Activity.java:3907) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at android.app.ActivityThread.deliverResults(ActivityThread.java:2492) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): ... 11 more

There is a lot wrong with this, but I think the first and most important thing for you to grasp is that the call to authorize is asynchronous.

This means that onComplete() gets called at a distant time in the future. Your facebook requests occur "after" the callback definition for onComplete() in the code, but seeing how it is asynchronous those calls are going to happen waaay before the network request for facebook login comes back which means you don't have an auth token when you make that request.

*I should add that DialogListener is a connivence class written by facebook to act as way to handle callbacks for async calls.

Both of the above answers are correct. You can just put the chunk of code that does the request into a separate method and call that method in onComplete(), ie

Facebook facebook = new Facebook("XXXXXasdasdajksdad");

TextView nText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nText = (TextView) this.findViewById(R.id.nameText);

    facebook.authorize(this, new String[] {"offline_access", "user_interests", "friends_interests"},

            new DialogListener() {
        public void onComplete(Bundle values) {
            getFriends();
            setContentView(R.layout.homepage);
        }

        public void onFacebookError(FacebookError error) {}

        public void onError(DialogError e) {}

        public void onCancel() {}
    });


}

private void getFriends(){

    JSONObject json_data = null;

    try
    {
        JSONObject response = Util.parseJson(facebook.request("me/friends")); // Get a friend information from facebook
        JSONArray jArray = response.getJSONArray("data");

        json_data = jArray.getJSONObject(0);
        String name = json_data.getString("name");
        Log.i("friend is", name);
        nText.setText(name);

    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (FacebookError e)
    {
        e.printStackTrace();
    }

}

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

    facebook.authorizeCallback(requestCode, resultCode, data);
}

Just call facebook.request("me/friends") from within onComplete and let the rest of the logic follow from that.

That way it will only call once the Facebook onject has an access token and not if there is an error or some such. You will probably want to use the other callbacks in the DialogListener for a better flow. Also think about maintaining that facebook object across the application if you plan on making more requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM