Android Facebook Graph api Basic information requesting

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

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

immediately after authentication, where the code is executed. Can someone point me in the right direction? I saw 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 described earlier 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

+4
source share
3 answers

Both of the above answers are correct. You can simply put the piece of code that makes the request into a separate method and call that method in onComplete (), i.e.

 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); } 
+1
source

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

This means that onComplete () is called at a remote time in the future. Your facebook requests appear after you define the callback for onComplete () in the code, but, seeing how it is asynchronous, these calls will be executed until the network request to enter facebook returns, which means that you don’t have authentication token when you make this request.

* I have to add that DialogListener is a compliance class written by facebook to act as a way to handle callbacks for asynchronous calls.

+4
source

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

Thus, it will be called only after Facebook onject has an access token, and not if there is an error or some of them. You will probably want to use other callbacks in DialogListener for a better flow. Also consider supporting this facebook object in your application if you plan on making more requests.

0
source

All Articles