Facebook实时更新在java示例 [英] facebook real time update in java example
问题描述
我正在开发一个使用Facebook Graph Api来收集有关公开帖子的信息的项目。有更新这个数据的时候需要更新这个帖子。我已经看到一个实时更新机制,使用回叫网址 https:// developers.facebook.com/docs/graph-api/reference/app/subscriptions/ 和 https://developers.facebook.com/docs/graph-api/real-time-updates/ ..但是我没有在java中做任何想法。请有人帮助我使用一个例子。
I'm working on a project which uses Facebook Graph Api to collect information about the public posts. There is a need to update this data Real-Time when changes come to this posts. i have seen a real time update mechanism using call back url https://developers.facebook.com/docs/graph-api/reference/app/subscriptions/ and https://developers.facebook.com/docs/graph-api/real-time-updates/ .. But i didnt get any idea of doing this in java. Please somebody help me using an example.
推荐答案
实时更新可用于页面和用户对象。他们中的两个需要访问相关页面或用户的标记(对于页面,您可以使用我/帐户查看它,或通过给您的应用程序提供页面访问权限)。
Real time updates are available for page and user objects.Both of them requires the access token of the respective page or user.(For page you can check it using me/accounts or get it via giving page access permissions to your app).
步骤1:您将需要一个公共ip,其中Facebook将发布更新。
步骤2:您需要设置您的fb应用程序以启动实时更新设置
Step 1: You will need a public ip where facebook will post the updates. Step 2: You need to setup your fb app to initiate real-time updates setup
String appId = //your app id
String token = //app token appId|appSecret
String callbackUrl = //your public url
PostMethod method = new PostMethod();
method.setURI(new URI("https://graph.facebook.com/" + appId +
"/subscriptions?callback_url=" + callbackUrl +
"&object=page&fields=feed&verify_token=streamInit&method=POST&access_token="+
token, false));
HttpClient httpClient = new HttpClient();
if (method != null) {
int statusCode = httpClient.executeMethod(method);
String response = new String(method.getResponseBody());
if (statusCode == HttpStatus.SC_OK) {
//Completed streaming initiation
}else{
//Streaming initialization failed"
}
}
} catch (Exception e) {
}
第3步:这将触发Facebook回调url验证。它可以是一个servlet或任何其他将处理发布到回调url的更新.Facebook将提供您需要重新发送的获取请求中的挑战代码。 p>
Step 3:This will trigger facebook callback url validation.It can be a servlet or any other which will process the updates posted to callback url.Facebook will provide a challenge code in a get request which you need to resend.
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map parametersMap = request.getParameterMap();
if (parametersMap.size() > 0) {
if (request.getParameter("hub.mode").equals("streamInit")) {
System.out.println("Verify Token: " + request.getParameter("hub.verify_token"));
System.out.println("Challenge number:" + request.getParameter("hub.challenge"));
String responseToClient = request.getParameter("hub.challenge");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write(responseToClient);
response.getWriter().flush();
response.getWriter().close();
//"Completed streaming setup on your url"
}
}
}
步骤3:将应用程序添加为页面选项卡,从该页面的实时更新中进行订阅。您将需要一个页面访问令牌
Step 3: Add your application as a page tab to subscribe from real time updates from that page.you will need a page Access token
String appId = //your application id
String pageId=//page id to subscribe from
PostMethod method =
new PostMethod("https://graph.facebook.com/" + pageId + "/tabs?app_id=" + appId +
"&method=POST&access_token=" + pageToken);
HttpClient httpClient = new HttpClient();
try {
int statusCode = httpClient.executeMethod(method);
String response = new String(method.getResponseBody());
if (statusCode == HttpStatus.SC_OK) {
// "Completed tab addition"
}else{
// "Tab adding failed"
}
} catch (Exception e) {
//"Tab adding failed"
}
步骤4:Facebook将通过发送请求将更新发送到您的回电url。
Step 4 : Facebook will post updates via post request to your call back url.
这篇关于Facebook实时更新在java示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!