Send App request to all friends in Facebook using 'Requests Dialog' in Android

8.2k Views Asked by At

I want to know how to send app request to all my facebook friends from android app. I tried in graph API. But, couldn't get it done.

https://graph.facebook.com/apprequests?ids=friend1,friend2&message='Hi'&method=post&access_token=ACCESS_TOKEN

I know this is a Duplicate question. But, couldn't find an answer yet. I'm getting this error on the above API.

"All users in param ids must have accepted TOS."

I hope there will be a way to send app request to all friends from mobile on a click. Please share it.

2

There are 2 best solutions below

9
On

Have you seen demo of "Hackbook" in the developer.facebook.com ?

You can refer HACKBOOK APP REQUEST FROM HERE.

You can achieve to post the app request to only one friend by below code.

Code:

Bundle params = new Bundle();

            JSONObject attachment = new JSONObject();
            JSONObject properties = new JSONObject();
            JSONObject prop1 = new JSONObject();
            JSONObject prop2 = new JSONObject();
            JSONObject media = new JSONObject();
            JSONStringer actions = null;
            try {
                attachment.put("name", "YOUR_APP");
                attachment.put("href", "http://www.google.com/");
                attachment.put("description", "ANY_TEXT");
                media.put("type", "image");
                media.put("src", "IMAGE_LINK");
                media.put("href", "http://www.google.com/");
                attachment.put("media", new JSONArray().put(media));
                prop1.put("text", "www.google.com");
                prop1.put("href", "http://www.google.com");
                properties.put("Visit our website to download the app", prop1);
               /* prop2.put("href", "http://www.google.com");
                properties.put("iTunes Link      ", prop2);*/
                attachment.put("properties", properties);
                Log.d("FACEBOOK", attachment.toString());

                actions = new JSONStringer().object()
                            .key("name").value("APP_NAME")
                            .key("link").value("http://www.google.com/").endObject();

            } catch (JSONException e) {
                e.printStackTrace();
            }

            System.out.println("ACTIONS STRING: "+actions.toString());
            System.out.println("ATTACHMENT STRING: "+attachment.toString());

            params.putString("actions", actions.toString());
            params.putString("attachment", attachment.toString()); // Original
            params.putString("to", "YOUR_FRIEND_FACEBOOK_ID");
            Utility.mFacebook.dialog(getParent(), "stream.publish", params,new PostDialogListener());



 public class PostDialogListener extends BaseDialogListener {
    @Override
    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            Toast.makeText(getApplicationContext(), ""+getResources().getString(R.string.facebook_response_msg_posted), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), ""+getResources().getString(R.string.facebook_response_msg_not_posted), Toast.LENGTH_SHORT).show();
        }
    }
}

Above code works perfect if you want to post the Apprequest only on One friend's wall. If you want to post on all then you have to make asynckTask which runs for all the friends post and post App request on all walls.

Update

Here is the link in PHP that have done same work to send request to all Facebook friends.

And [here it is clearly explained3 that it is blocked by Facebook to send a Friend Request to more then 15-20 friends.

Now, you have to only one option to do it is, use above code in AsyncTask to send Friend Request to all Friends One-by-One.

2
On

The error message you receive ("All users in param ids must have accepted TOS") is because you are trying to send an app generated request to a user who is not connected to your app.

See the developer docs here.

Requests sent with the request dialog and app generated requests are different and you can't use app generated requests to invite users to your app.

Sending Facebook app requests are not available via the graph api. You can use the app requests java-script dialog to send the request though, you would just need to specify the user's id in the "to" property as detailed in the documentation.

Sample function:

<script>
  FB.init({ appId: '**appId**', status: true, cookie: true, xfbml : true });

  function sendRequest(to) {
    FB.ui({method: 'apprequests', to: to, message: 'You should learn more about this awesome site.', data: 'tracking information for the user'});
    return false;
  }
</script>

Then just wire an onclick for each image to something like onclick="return sendRequest('**friendId**');"

Also you can call this function in javascript: It will give you all friends with photos. Also group of friends who are currently using same app. You can send request to any of them.

function sendRequestViaMultiFriendSelector() {
    FB.ui({
        method: 'apprequests',
        message: "You should learn more about this awesome site."
    });     
}

See Facebook Friend Request - Error - 'All users in param ids must have accepted TOS'