Facebook:发送应用邀请

时间:2011-05-20 13:45:21

标签: facebook facebook-graph-api restfb

如何通过Graph API发送使用应用的邀请?我查看了参考文献,找不到合适的东西。任何线索?

3 个答案:

答案 0 :(得分:5)

HTML / javascript示例:

<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>

您可以将javascript与链接的点击事件等相关联。

您可以在此处找到详细信息:
http://developers.facebook.com/blog/post/464/

答案 1 :(得分:4)

如果你需要使用restfb,我可以试试这个:

这是一个自包含的junit测试,用于检索身份验证令牌并发布apprequest。不需要用户生成auth_token,但您只能向已经授权您的应用的用户发送此类应用。

依赖关系是JUnit,Commons HttpClient和restfb本身。

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;
    }
}

答案 2 :(得分:0)

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

您可以使用上述链接

中所述的对话框