Tag Archives: facebook

Facebook Mobile HACK – Tel Aviv

Once in while I have a chance to attend an event that actually leaves an impression, “Facebook Mobile Hack” was one of those.

The event lasted for 12 hours ! it had technical lectures from Facebook R&D and what I think was the cream the la creme, the hackaton.

If you had a chance to read Mark’s manifest then you understand that intense hack days like these are rooted deep in the Facebook culture and all I can say that it was magnificent.

Beer in one end, coffee on the other I sat in one of the corners of the hanger and started coding for about 5 hours, sitting on my chair and feeling how the adrenaline is pumping. At the end of the hackaton all of the contenders went to stage, pitched the idea to all of us and to the panel of judges (Yossi Vardi,  influential VCs, Angels and techies).

The winner of the event was a cool app called Instapeace which connects FB and Instagram. Unfortunately I didn’t win, but you can see the end result on the Android Market – I Want To Buy

For those of you interested in the technical stuff you can download the presentations from here.


Leave a comment

Filed under Building

Real time Facebook notification from app engine

It’s hard  to find a good example for Facebook authentication on app engine using java.

I’ll try to do the following:

  1. Explain the general flow for achieving  Real time notification on app engine.
  2. Show some code snippets for implementing the above flow.

General Flow + Code

Step 1

Use the callback mechanism of Facebook to start the authentication process.
http://www.facebook.com/dialog/oauth?client_id=<client_id&gt; &display=touch&scope=offline_access,read_stream&redirect_uri=<callback_url>

I’m calling to this URL from a mobile app,therefore I use display=touch, in addition pay attention that I’m asking from the user to grant me offline access, I’m assuming that I’ll update the user long after the authentication token will expire.

Step 2

I’ve prepared a callback get method on my app engine. Facebook calls my callback url passing to it a code. The code means the user granted me permissions.

Map<String, String[]> parameters = req.getParameterMap();
if (parameters.containsKey("code")) {
facebookAuthCode = ... // Get 'code' value

Pay attention that the code is not the access token (which is the holly grail), the access token will allow us to act on behalf of the user.

You need to call another url with the code you extracted, the response will be the access token.

String facebookAuthUrl = String.format("https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s",
							Constants.FACEBOOK_APP_CODE, reconstructedURL,
							Constants.FACEBOOK_APP_SECRET, facebookAuthCode);
URL localUrl = new URL(facebookAuthUrl);
URLConnection conn = localUrl.openConnection();
BufferedReader rd = null;
try {
	// Get the response
	rd = new BufferedReader(
	new InputStreamReader(conn.getInputStream()));

The easiest way to build a valid callback url is to use the request object.

URL baseURL = new URL(req.getScheme(), req.getServerName(),req.getRequestURI());

Now we register for real time notification.

Step 3

We need to post our data to Facebook with the appropriate parameters. They should include:

  • object – To which object in the social graph we want to listen to.
  • fields – Special fields that make up the object.
  • callback_url – Call this URL with post. In our case it is baseURL we used above.
  • verify_token – A token that will be sent back during hub mode.
  • access_token – built from facebook_app_code|facebook_app_secret . You should get these two values from your Facebook account. Pay attention to ‘|’.
// Use String builder instead of just regular string.
String data = URLEncoder.encode("object", "UTF-8") + "="
				+ URLEncoder.encode("user", "UTF-8");
data += "&" + URLEncoder.encode("fields", "UTF-8") + "="
				+ URLEncoder.encode("home", "UTF-8");
// Send real time updates to this URL.
data += "&" + URLEncoder.encode("callback_url", "UTF-8") + "="
				+ URLEncoder.encode(reconstructedURL.toString(), "UTF-8");
// A token used to verify that it is actually me who is running the
// verification process.
data += "&" + URLEncoder.encode("verify_token", "UTF-8") + "="
				+ URLEncoder.encode(Constants.FACEBOOK_VERIFY_TOKEN, "UTF-8");
data += "&" + URLEncoder.encode("access_token", "UTF-8") + "="
				+ Constants.FACEBOOK_APP_CODE + "|"
				+ Constants.FACEBOOK_APP_SECRET;
// Subscribe by doing a post.
URL url = new URL(String.format(
				Constants.FACEBOOK_APP_CODE, accessToken));

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());


Almost done here the final stage is the hub mode.

Step 4

Facebook is using hub mode to do a final verification of the process. You will receive back a verification token that you sent during your registration process (step 3), you will have to make sure it is equal to what you sent and then response with a challenge string that Facebook sent.

String verifyToken = (String) parameters.get("hub.verify_token")[0];
if (verifyToken.equals(Constants.FACEBOOK_VERIFY_TOKEN)) {
	log.info("hub.mode test is valid");

That’s it you are done you are now registered.


Filed under Uncategorized