Permission Error - Trying to get friends using android facebook sdk

I'm trying to add a feature to my Android app that allows users to โ€œcheckโ€ with other people that are marked for verification. I have a validation method that works without problems and can mark it by adding the user ID as a parameter (see code below).

public void postLocationTagged(String msg, String tags, String placeID, Double lat, Double lon) { Log.d("Tests", "Testing graph API location post"); String access_token = sharedPrefs.getString("access_token", "x"); try { if (isSession()) { String response = mFacebook.request("me"); Bundle parameters = new Bundle(); parameters.putString("access_token", access_token); parameters.putString("place", placeID); parameters.putString("Message",msg); JSONObject coordinates = new JSONObject(); coordinates.put("latitude", lat); coordinates.put("longitude", lon); parameters.putString("coordinates",coordinates.toString()); parameters.putString("tags", tags); response = mFacebook.request("me/checkins", parameters, "POST"); Toast display = Toast.makeText(this, "Checkin has been posted to Facebook.", Toast.LENGTH_SHORT); display.show(); Log.d("Tests", "got response: " + response); if (response == null || response.equals("") || response.equals("false")) { Log.v("Error", "Blank response"); } } else { // no logged in, so relogin Log.d(TAG, "sessionNOTValid, relogin"); mFacebook.authorize(this, PERMS, new LoginDialogListener()); } } catch(Exception e) { e.printStackTrace(); } } 

This works great (I sent it in case it helps someone else!), The problem I am facing is trying to create a list of friends of users so that they can choose the friends they want the tag to. I have a GetFriends method (see below), which I then intend to use to create an AlertDialog that the user can choose one of which, in turn, will give me an identifier for use in the above postLocationTagged method.

 public void getFriends(CharSequence[] charFriendsNames,CharSequence[] charFriendsID, ProgressBar progbar) { pb = progbar; try { if (isSession()) { String access_token = sharedPrefs.getString("access_token", "x"); friends = charFriendsNames; friendsID = charFriendsID; Log.d(TAG, "Getting Friends!"); String response = mFacebook.request("me"); Bundle parameters = new Bundle(); parameters.putString("access_token", access_token); response = mFacebook.request("me/friends", parameters, "POST"); Log.d("Tests", "got response: " + response); if (response == null || response.equals("") || response.equals("false")) { Log.v("Error", "Blank response"); } } else { // no logged in, so relogin Log.d(TAG, "sessionNOTValid, relogin"); mFacebook.authorize(this, PERMS, new LoginDialogListener()); } } catch(Exception e) { e.printStackTrace(); } } 

When I look at the answer in the journal, it reads:

"got the answer: {" error ": {" type ":" OAuthException "," message ":" (# 200) Permission error "}}"

I looked at the GraphAPI documentation and looked for similar questions, but to no avail! I'm not sure if I need to request additional permissions for the application, or if this is something you are simply not allowed to do! Any help / suggestions would be greatly appreciated.

+3
source share
3 answers

The solution is to implement RequestListener when accessing the Facebook API. I have a new getFriends () method (see below) that uses AsyncGacebookRunner to request data.

 public void getFriends(CharSequence[] charFriendsNames,String[] sFriendsID, ProgressBar progbar) { try{ //Pass arrays to store data friends = charFriendsNames; friendsID = sFriendsID; pb = progbar; Log.d(TAG, "Getting Friends!"); //Create Request with Friends Request Listener mAsyncRunner.request("me/friends", new FriendsRequestListener()); } catch (Exception e) { Log.d(TAG, "Exception: " + e.getMessage()); } } 

AsyncFacebookRunner executes the request using the FriendsRequestListener user method (see below), which implements the RequestListener class;

 private class FriendsRequestListener implements RequestListener { String friendData; //Method runs when request is complete public void onComplete(String response, Object state) { Log.d(TAG, "FriendListRequestONComplete"); //Create a copy of the response so i can be read in the run() method. friendData = response; //Create method to run on UI thread FBConnectActivity.this.runOnUiThread(new Runnable() { public void run() { try { //Parse JSON Data JSONObject json; json = Util.parseJson(friendData); //Get the JSONArry from our response JSONObject JSONArray friendArray = json.getJSONArray("data"); //Loop through our JSONArray int friendCount = 0; String fId, fNm; JSONObject friend; for (int i = 0;i<friendArray.length();i++){ //Get a JSONObject from the JSONArray friend = friendArray.getJSONObject(i); //Extract the strings from the JSONObject fId = friend.getString("id"); fNm = friend.getString("name"); //Set the values to our arrays friendsID[friendCount] = fId; friends[friendCount] = fNm; friendCount ++; Log.d("TEST", "Friend Added: " + fNm); } //Remove Progress Bar pb.setVisibility(ProgressBar.GONE); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FacebookError e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } 

Feel free to use any of these codes in your projects or ask any questions about it.

+2
source

You may need the following permissions:

  • user_checkins
  • friends_checkins
  • read_friendlists
  • manage_friendlists
  • publish_checkins

Check the related API documents. Before doing this, make sure which line causes this permission error and tries to fix it.

+2
source

You can private static final String[] PERMISSIONS = new String[] {"publish_stream","status_update",xxxx}; xxx send messages

0
source

Source: https://habr.com/ru/post/1495936/


All Articles