how implement facebook realtime api
- configure facebook app , install app on facebook pages/users want updates for.
we need maintain callback url facebook able post updates. jersey based implementation example:
@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() { // 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 json string desired purpose } }
install app page , subscribe page updates
public class facebookrealtimesubscriber { private accesstoken appaccesstoken = null; private string appsecret = // app secret private string useraccesstoken = // access token user owns page, generated using app private string applicationid = // application id private string callbackurl = "<your context root>/social/facebook/update"; private string pagename = // page name want install app private facebookclient client = null; private final string subscribedappsedge = "/subscribed_apps"; private final string appsubscriptions = "/subscriptions"; private final string verifytoken = "anyrandomverifytoken"; // below fields can subscribed 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(); (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 page object obj = client.publish(pageid + subscribedappsedge, jsonobject.class, parameter.with("id", long.valueof(pageid))); system.out.println(obj.tostring()); // list subscriptions app obj = client.fetchobject(pageid + subscribedappsedge, jsonobject.class); system.out.println(obj.tostring()); // subscribe page updates 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); // subscriptions app obj = client.fetchobject(applicationid + appsubscriptions, jsonobject.class); system.out.println(obj); } }
Comments
Post a Comment