Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

p5facebook

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 3.21 KB  |  views: 179  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* VALUES YOU NEED TO PROVIDE */
  2.  
  3. // application api key and secret
  4. String fbApiKey = "";
  5. String fbApiSecret = "";
  6.  
  7. // a comma separated (no spaces!) list of user ids
  8. String fbUserIDs = "";
  9.  
  10. /* other settings */
  11.  
  12. // Facebook RESTful API
  13. String fbRestServer = "http://api.facebook.com";
  14. String fbRestNode = "/restserver.php";
  15.  
  16.  
  17. XMLElement[] usersXml;
  18. int currentUser = 0;
  19.  
  20.  
  21. void setup ()
  22. {
  23.   size( 300, 200 );
  24.  
  25.   // the details / params in here define what will be read, see:
  26.   // http://wiki.developers.facebook.com/index.php/Users.getInfo
  27.   String xmlResponse = fbCallMethod( new String[] {
  28.     "method=facebook.Users.getInfo",  
  29.     "uids=" + fbUserIDs,
  30.     "fields=uid,first_name,last_name", // see link above for more options
  31.     "format=XML"
  32.   });
  33.  
  34.   if ( xmlResponse == null )  // an error occured
  35.   {
  36.       exit();
  37.       return;
  38.   }
  39.  
  40.   XMLElement xml = new XMLElement( xmlResponse );
  41.   usersXml = xml.getChildren( "user" );
  42.  
  43.   fill( 0 );
  44.   textFont( createFont( "sans-serif", 24 ) );
  45.   textAlign( CENTER );
  46.   frameRate( 1 );
  47. }
  48.  
  49.  
  50. void draw ()
  51. {
  52.   background( 255 );
  53.  
  54.   String full_name = usersXml[currentUser].getChild("first_name").getContent();
  55.   full_name += " " + usersXml[currentUser].getChild("last_name").getContent();
  56.  
  57.   text( full_name, width/2, height/2 );
  58.  
  59.   currentUser++;
  60.   currentUser %= usersXml.length; // modulo, wrap around
  61. }
  62.  
  63. /**
  64.  *  Place a Facebook call (GET request) using Processing API ( loadStrings(), join() )
  65.  */
  66. String fbCallMethod ( String[] args )
  67. {
  68.   String[] params = new String[args.length + 3];
  69.   System.arraycopy( args, 0, params, 0, args.length );
  70.   params[params.length-3] = "api_key=" + fbApiKey;
  71.   params[params.length-2] = "call_id=" + System.currentTimeMillis();
  72.   params[params.length-1] = "v=1.0";
  73.  
  74.   String sig = fbGenerateSIG ( params );
  75.   String paramString = join( params, "&" ) + "&sig=" + sig;
  76.  
  77.   String[] lines = loadStrings( fbRestServer + fbRestNode + "?" + paramString );
  78.  
  79.   if ( lines == null )
  80.   {
  81.       println( "OUCH, nothing to read from that URL:\n" + fbRestServer + fbRestNode + "?" + paramString );
  82.       return null;
  83.   }
  84.  
  85.   String response = join( lines, "\n" );
  86.  
  87.   return response;
  88. }
  89.  
  90. /**
  91.  *  Generate a call signature, see:
  92.  *  http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application
  93.  *  http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Desktop_Applications
  94.  */
  95. String fbGenerateSIG ( String[] args )
  96. {
  97.   java.util.Arrays.sort( args );
  98.   String argString = join( args, "" );
  99.   argString += fbApiSecret;
  100.  
  101.   return md5Encode( argString );
  102. }
  103.  
  104. /**
  105.  * MD5 encode a String using Processing API ( hex() )
  106.  */
  107. String md5Encode ( String data )
  108. {
  109.   java.security.MessageDigest digest = null;
  110.   try {
  111.     digest = java.security.MessageDigest.getInstance("MD5");
  112.   }
  113.   catch ( java.security.NoSuchAlgorithmException nsae ) {
  114.     nsae.printStackTrace();
  115.   }
  116.     digest.update( data.getBytes() );
  117.   byte[] hash = digest.digest();
  118.  
  119.   StringBuilder hexed = new StringBuilder();
  120.  
  121.   for ( int i = 0; i < hash.length; i++ )
  122.   {
  123.     hexed.append( hex( hash[i], 2 ) );
  124.   }
  125.  
  126.   return hexed.toString().toLowerCase();
  127. }