Facebook:发送应用邀请 [英] Facebook: send an app invitation

查看:145
本文介绍了Facebook:发送应用邀请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Graph API发送使用应用程序的邀请?我看了一下参考书,找不到合适的东西。任何线索?

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?

推荐答案

如果您需要使用restfb,就像从标签中得到的一样,您可以尝试以下方式:

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

这是一个独立的junit测试,它检索认证令牌并发布一个apprequest。没有用户生成auth_token是必需的,但您只能向已经授权您的应用程序的用户发送这样的应用请求。

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.

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

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

这篇关于Facebook:发送应用邀请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆