如何使用回调实现 Facebook 实时更新订阅 URL

How to implement Facebook Realtime Update Subscription with callback URL

如何实现Facebook Realtime API

  1. 配置 Facebook 应用程序,然后在 Facebook pages/users 上安装您想要更新的应用程序。
  2. 我们需要维护回调 URL 以便 Facebook 能够 post 更新。以基于 Jersey 的实现为例:

            @Path("/social/facebook/update")
            public class FacebookRealtimeAPIResource
            {
                private static final String HUB_MODE = "hub.mode";
                private static final String HUB_CHALLENGE = "hub.challenge";
                private static final String HUB_VERIFY_TOKEN = "hub.verify_token";
    
                public FacebookRealtimeAPIResource()
                {
                    // any desired implementation here
                }
    
                @GET
                @Produces(MediaType.TEXT_HTML)
                public void validateFacebookRequest(
                @DefaultValue("") @QueryParam(HUB_MODE) String hubMode,
                @DefaultValue("") @QueryParam(HUB_CHALLENGE) String hubChallenge,
                @DefaultValue("") @QueryParam(HUB_VERIFY_TOKEN) String hubVerifyToken,
                @Context HttpServletRequest request,
                @Context HttpServletResponse response)
                {
    
                    try
                    {
                        // hubVerifyToken based validation if desired       
                        response.setStatus(HttpServletResponse.SC_OK);
                        response.getWriter().write(hubChallenge);
                        response.getWriter().flush();
                        response.getWriter().close();
                    }
                    catch (IOException exc)
                    {
                        throw new WebApplicationException(Response.Status.BAD_REQUEST);
                    }
                }
    
                @POST
                @Consumes(MediaType.APPLICATION_JSON)
                public void processFacebookRealtimeUpdate(@Context HttpServletRequest request, InputStream inputStream)
                {
                    StringBuilder sb = new StringBuilder();
                    String newLine = System.getProperty("line.separator");
                    String line;
                    String json = "";
    
                    try
                    {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, request.getCharacterEncoding()));
                        while ((line = reader.readLine()) != null)
                        sb.append(line).append(newLine);
                    }
                    catch (Exception exc)
                    {
                        throw new WebApplicationException(Response.Status.BAD_REQUEST);
                    }
                    json = sb.toString(); // use this json string for desired purpose
                }
            }
    
  3. 为页面安装应用程序然后订阅页面更新

            public class FacebookRealtimeSubscriber
            {
                private AccessToken appAccessToken = null;
                private String appSecret = // your app secret
                private String userAccessToken = // access token for user that owns the page, generated using your app
                private String applicationId = // your application id
                private String callbackURL = "<your context root>/social/facebook/update";
                private String pageName = // page name you want to install app for
                private FacebookClient client = null;
                private final String subscribedAppsEdge = "/subscribed_apps";
                private final String appSubscriptions = "/subscriptions";
                private final String verifyToken = "AnyRandomVerifyToken";
                // below are all the fields that can be subscribed for page object
                private final String pageFields = "feed,ratings,name,picture,category,description,founded,company_overview,conversations,mission,products,general_info,location,hours,parking,public_transit,phone,email,website,attire,payment_options,culinary_team,general_manager,price_range,restaurant_services,restaurant_specialties,videos,release_date,genre,starring,screenplay_by,directed_by,produced_by,studio,awards,plot_outline,network,season,schedule,written_by,band_members,hometown,current_location,record_label,booking_agent,press_contact,artists_we_like,influences,band_interests,bio,affiliation,birthday,personal_info,personal_interests,members,built,features,mpg,checkins,productlists";
    
                public static void main(String[] args)
                {
                    new FacebookRealtimeSubscriber().subscribe();
                }
    
                private void subscribe()
                {
                    String pageAccessToken = "";
                    String pageId = "";
                    client = new DefaultFacebookClient(Version.VERSION_2_3);
                    appAccessToken = client.obtainAppAccessToken(applicationId, appSecret);
                    client = new DefaultFacebookClient(userAccessToken, Version.VERSION_2_3);
                    Connection<Account> pages = client.fetchConnection("me/accounts", Account.class);
                    List<Account> accounts = pages.getData();
                    for (Account account : accounts)
                    {
                        if (pageName.equals(account.getName()))
                        {
                            pageAccessToken = account.getAccessToken();
                            pageId = account.getId();
                        }
                    }
                    client = new DefaultFacebookClient(pageAccessToken, appSecret, Version.VERSION_2_3);
                    // subscribe app for page
                    Object obj = client.publish(pageId + subscribedAppsEdge, JsonObject.class, Parameter.with("id", Long.valueOf(pageId)));
                    System.out.println(obj.toString());
                    // list subscriptions for app
                    obj = client.fetchObject(pageId + subscribedAppsEdge, JsonObject.class);
                    System.out.println(obj.toString());
                    // subscribe for page updates for app
                    client = new DefaultFacebookClient(appAccessToken.getAccessToken(), appSecret, Version.VERSION_2_3);
                    obj = client.publish(applicationId + appSubscriptions,
                    JsonObject.class,
                    Parameter.with("object", "page"),
                    Parameter.with("callback_url", callbackURL),
                    Parameter.with("fields", pageFields),
                    Parameter.with("verify_token", verifyToken));
                    System.out.println(obj);
                    // get subscriptions for app
                    obj = client.fetchObject(applicationId + appSubscriptions, JsonObject.class);
                    System.out.println(obj);
                }
            }