views:

3847

answers:

2

Hello there,

I am going to use RESTful Web Services and HttpClient to access Facebook API REST Server.

Am somewhat of a newbie to REST and Facebook APIs...

Question(s):

Verification / Authorization

(1) If I have a session key sent by a client app, how do I verify and authenticate that the user exists and then query for his / her friends on the server side?

How can I be access these Facebook RESTful end points:

http://wiki.developers.facebook.com/index.php/Users.getInfo

and

http://wiki.developers.facebook.com/index.php/Friends.getLists

via a HTTP GET Request? Meaning, what does the full URL look like including parameters?

(2) What would the full RESTful URL look like to grab the APIs (which I have listed above)?

Posting to a Friend's Wall

(3) After verification / authorization, querying users friends, how (which API) would I use to a post to a Friend's Wall?

(4) Is there any additional parameters that I need to append to the Facebook RESTful Server's URL?

HTTP Client

(5) Do I include the RESTful web service calls to these Facebook APIs inside my Java program through HttpClient?

Happy programming and thank you for taking the time to read this...

+1  A: 

I can't answer all your questions but the method calls are made via http://api.facebook.com/restserver.php so a call to users.getInfo looks like this

http://api.facebook.com/restserver.php?method=users.getinfo

You also need to pass in your api key and any other parameters the method needs. But rather than make the http calls yourself there must be some Java library that abstracts all this away for you.

As for this being a REST API - there's one web service endpoint with method scoping in the URL and all calls are made via HTTP GET or POST.

Frankly, this is RPC over HTTP and about as far from REST as you can get (no pun intended!). Facebook should change their API documentation, it's just plain wrong.

Bedwyr Humphreys
A: 

Hey,

In terms of creating the URL, I've used this code which seems to work pretty well...

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

// Written by Stuart Davidson, www.spedge.com
public class JSONComm 
{   
private final String JSON_URL = "http://api.facebook.com/restserver.php";
private final String fbSecretKey = "xxx";
private final String fbApiKey = "xxx";
private final String fbApiId = "xxx";

private int callId = 0;

public int getNextCall() { callId++; return callId; }
public String getApiKey() { return fbApiKey; }
public String getApiId() { return fbApiId; }

public String getRestURL(HashMap<String, String> args)
{
    String url = JSON_URL + "?";
    for(String arg : args.keySet()) { url = url + arg + "=" + args.get(arg) + "&"; }

    String sig = getMD5Hash(args);
    url = url + "sig=" + sig;

    return url;
}

public String getMD5Hash(HashMap<String, String> args)
{   
    String message = "";

    Vector<String> v = new Vector<String>(args.keySet());
    Collections.sort(v);
    Iterator<String> it = v.iterator();

    while(it.hasNext()) 
    { 
        String tmp = it.next();
        message = message + tmp + "=" + args.get(tmp);
    }

    message = message + fbSecretKey;

    try{
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data = message.getBytes(); 
        m.update(data,0,data.length);
        BigInteger i = new BigInteger(1,m.digest());
        return String.format("%1$032X", i).toLowerCase();
    }
    catch(NoSuchAlgorithmException nsae){ return ""; }
}
}

Make sure you see the critical components - the fact that the arguments are alphabetically sorted, and that the whole thing is encrypted using MD5, but the string that is encrypted is slightly different than the URL string.

Also note that the API keys need to be filled in!

So, to get the URL for the method User.getInfo and return the first and last names, I'd do the following...

public String getFbURL(String callback, Long playerId)
{
    HashMap<String, String> args = new HashMap<String, String>();
    args.put("api_key", jsonComm.getApiKey());
    args.put("call_id", "" + jsonComm.getNextCall());
    args.put("v", "1.0");
    args.put("uids", "" + playerId);
    args.put("fields", "first_name,last_name");
    args.put("format", "JSON");
    args.put("method", "Users.getInfo");
    args.put("callback", "" + callback);

    return jsonComm.getRestURL(args);
}

Hope this helps :)

Spedge