8

How can I send an invitation to use an app with Graph API? I looked at the reference, and can't find something that fits. Any clue?

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
  • 2
    It's not possible for now. The old fbml and the new requests dialog are the only way to send requests. – DannyKK May 20 '11 at 14:28
  • So it's not possible to send a request from our servers? – Alexis Dufrenoy May 20 '11 at 15:07
  • 2
    @Traroth: that's correct. The only way to send a direct invite with a POST to {user_id}/apprequests, and the user has to have already installed your application and given permissions to access that endpoint. – typeoneerror Sep 06 '11 at 16:16

3 Answers3

5

A HTML/javascript example:

<html>
  <head>
  <title>My Great Website</title>
  </head>
  <body>
  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/en_US/all.js">
  </script>
  <script>
    FB.init({ 
      appId:'YOUR_APP_ID', cookie:true, 
      status:true, xfbml:true 
    });

    FB.ui({ method: 'apprequests', 
      message: 'Here is a new Requests dialog...'});
  </script>
  </body>
</html>

You can associate the javascript to click event of a link, etc.

You can find the details here:
http://developers.facebook.com/blog/post/464/

amit_saxena
  • 7,450
  • 5
  • 49
  • 64
4

If you need to use restfb, as I got from your tags, you can try this:

This is a self contained junit test that retrieves the authentication token and posts an apprequest. No user generated auth_token is required but you can only send apprequests like this to users that already have authorized your app.

Dependencies are JUnit, Commons HttpClient and restfb itself.

package com.apprequesttest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.Test;

import com.restfb.DefaultFacebookClient;
import com.restfb.Facebook;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;

public class AppRequestTest
{
    @Test
    public void testAppRequest() throws Exception
    {
        String appId = "YOUR_APP_ID";
        String appSecret = "YOUR_ACCESS_TOKEN";
        String tokenUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&client_secret=" + appSecret
                + "&grant_type=client_credentials";
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(tokenUrl);

        client.executeMethod(method);
        String rawAccessToken = new String(method.getResponseBody());

        String accessToken = rawAccessToken.split("=")[1];

        FacebookClient facebookClient = new DefaultFacebookClient(accessToken);

        String apprequestCall = "TARGET_USER_ID/apprequests";

        AppRequestResponse apprequest = facebookClient.publish(apprequestCall,
                                                               AppRequestResponse.class,
                                                               Parameter.with("message", "BANANAMSG"),
                                                               Parameter.with("data", "BANANADATA"));

        System.out.println(apprequest.request);
        System.out.println(apprequest.to);

    }

    /**
     * Couldn't find any beans that would acomodate this response on restfb so we create our own here
     * Looks like "to" is an array but this works well enough for the sake of the example 
     */
    public static class AppRequestResponse extends FacebookType
    {
        private static final long serialVersionUID = 1L;

        public AppRequestResponse()
        {
            // Empty
        }

        @Facebook
        public String request;

        @Facebook
        public String to;
    }
}
garbelini
  • 468
  • 3
  • 10
  • @garbelin...I tried your code and was able to send the invitation, but I have small problem. I keep getting this exception. I posted a question for the same http://stackoverflow.com/questions/10460014/facbook-post-userid-apprequest-throws-an-exception-invitation-is-sent-though – Santhosh May 05 '12 at 08:17
  • @skokal01 I never got those empty 403 errors. To me it smells like a network issue. – garbelini May 07 '12 at 09:57
  • Hi, I am getting 322391074505234 as request id and xxxx as to field. but I can't see the actual invitation when I check from the user account. what might be my missing point? – kitokid May 29 '12 at 03:32
  • and I can't use the access token generated with the code above. if use, i hit 403 errors. So, I have to use access token that I have granted by user with the normal flow. – kitokid May 29 '12 at 03:50
  • I can see 1 in Apps and Games. but can't see the app which send this invitation. Is there any settings that I need to set up my app. Actually my app is redirected to other url. not in facebookapps. – kitokid May 29 '12 at 04:02
  • just my mistake, cause my app is web type. not the canvas type. when I set as canvas type, I can see all the invitations. Thanks. for the people who might face the situation like me, pls check this link.http://stackoverflow.com/questions/9185009/any-example-of-sending-apprequest-app-to-user – kitokid May 29 '12 at 06:07
0

http://developers.facebook.com/docs/reference/dialogs/requests/

You can use dialogs as described in the above link

fmucar
  • 14,361
  • 2
  • 45
  • 50