Facebook Graph API
in
Programming Questions
•
1 year ago
Hi, I want to integrate my own facebook profile into Processing using the Graph API, to create something like this:
http://www.youtube.com/watch?v=ZL62fF6Uzjo
Here is the code I'm using:
// application api key and secret
String fbApiKey = "mykey";
String fbApiSecret = "mysecretapi";
// a comma separated (no spaces!) list of user ids
String fbUserIDs = "myid";
/* other settings */
//
// Facebook RESTful API
String fbRestServer = "";
String fbRestNode = "";
XMLElement[] usersXml;
int currentUser = 0;
void setup ()
{
size( 300, 200 );
// the details / params in here define what will be read, see:
String xmlResponse = fbCallMethod( new String[] {
"method=facebook.Users.getInfo",
"uids=" + fbUserIDs,
"fields=uid,first_name,last_name", // see link above for more options
"format=XML"
});
if ( xmlResponse == null ) // an error occured
{
exit();
return;
}
XMLElement xml = new XMLElement( xmlResponse );
usersXml = xml.getChildren( "user" );
fill( 0 );
textFont( createFont( "sans-serif", 24 ) );
textAlign( CENTER );
frameRate( 1 );
}
void draw ()
{
background( 255 );
String full_name = usersXml[currentUser].getChild("first_name").getContent();
full_name += " " + usersXml[currentUser].getChild("last_name").getContent();
text( full_name, width/2, height/2 );
currentUser++;
currentUser %= usersXml.length; // modulo, wrap around
}
/**
* Place a Facebook call (GET request) using Processing API ( loadStrings(), join() )
*/
String fbCallMethod ( String[] args )
{
String[] params = new String[args.length + 3];
System.arraycopy( args, 0, params, 0, args.length );
params[params.length-3] = "api_key=" + fbApiKey;
params[params.length-2] = "call_id=" + System.currentTimeMillis();
params[params.length-1] = "v=1.0";
String sig = fbGenerateSIG ( params );
String paramString = join( params, "&" ) + "&sig=" + sig;
String[] lines = loadStrings( fbRestServer + fbRestNode + "?" + paramString );
if ( lines == null )
{
println( "OUCH, nothing to read from that URL:\n" + fbRestServer + fbRestNode + "?" + paramString );
return null;
}
String response = join( lines, "\n" );
return response;
}
/**
* Generate a call signature, see:
*/
String fbGenerateSIG ( String[] args )
{
java.util.Arrays.sort( args );
String argString = join( args, "" );
argString += fbApiSecret;
return md5Encode( argString );
}
/**
* MD5 encode a String using Processing API ( hex() )
*/
String md5Encode ( String data )
{
java.security.MessageDigest digest = null;
try {
digest = java.security.MessageDigest.getInstance("MD5");
}
catch ( java.security.NoSuchAlgorithmException nsae ) {
nsae.printStackTrace();
}
digest.update( data.getBytes() );
byte[] hash = digest.digest();
StringBuilder hexed = new StringBuilder();
for ( int i = 0; i < hash.length; i++ )
{
hexed.append( hex( hash[i], 2 ) );
}
return hexed.toString().toLowerCase();
}
And the Error messages I get:
java.lang.IllegalArgumentException: Prefix string too short
at java.io.File.createTempFile(File.java:1782)
at java.io.File.createTempFile(File.java:1828)
at processing.app.Base.createTempFolder(Base.java:1566)
at processing.app.Sketch.makeTempFolder(Sketch.java:1112)
at processing.mode.java.JavaBuild.build(JavaBuild.java:135)
at processing.mode.java.JavaMode.handleRun(JavaMode.java:176)
at processing.mode.java.JavaEditor$20.run(JavaEditor.java:481)
at java.lang.Thread.run(Thread.java:662)
1