<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>.larre &#187; code</title> <atom:link href="http://www.larre.com/tag/code/feed/" rel="self" type="application/rss+xml" /><link>http://www.larre.com</link> <description>technology, innovation and media</description> <lastBuildDate>Fri, 29 Jan 2010 21:26:46 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.6</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Building a Facebook Application in 15 minutes</title><link>http://www.larre.com/2007/10/10/building-a-facebook-application-in-15-minutes/</link> <comments>http://www.larre.com/2007/10/10/building-a-facebook-application-in-15-minutes/#comments</comments> <pubDate>Wed, 10 Oct 2007 09:29:07 +0000</pubDate> <dc:creator>larre</dc:creator> <category><![CDATA[howto]]></category> <category><![CDATA[programming]]></category> <category><![CDATA[app]]></category> <category><![CDATA[application]]></category> <category><![CDATA[code]]></category> <category><![CDATA[development]]></category> <category><![CDATA[facebook]]></category> <category><![CDATA[integration]]></category> <category><![CDATA[java]]></category> <category><![CDATA[jsp]]></category> <category><![CDATA[platform]]></category><guid
isPermaLink="false">http://larre.wordpress.com/2007/10/10/building-a-facebook-application-in-15-minutes/</guid> <description><![CDATA[So, after fowa and 90k+ developers in mind, I just had to go ahead and implement a Facebook application. Here is the code and how I implemented it.
See also: Reflections after having implemented Facebook Connect and Google Friend Connect on my blog
First of all my goal was to use an existing web application, write a [...]]]></description> <content:encoded><![CDATA[<p>So, after <a
href="http://www.larre.com/2007/10/09/future-of-web-apps/">fowa</a> and 90k+ developers in mind, I just had to go ahead and implement a Facebook application. Here is the code and how I implemented it.</p><p><strong>See also: <a
href="http://www.larre.com/2008/12/18/reflections-after-having-implemented-facebook-connect-and-google-friend-connect-on-my-blog/">Reflections after having implemented Facebook Connect and Google Friend Connect on my blog</a></strong></p><p>First of all my goal was to use an existing web application, write a facebook integration module that communicates with the Facebook APIs, extracting some information/data and presenting them inside the existing web application. The web application I used is based on a j2ee framework (jsp, java). The architecture is much like <a
href="http://struts.apache.org">Struts</a> The first thing to do was to <a
href="http://developers.facebook.com/get_started.php">get the Developer Application</a> to my existing Facebook account. Then I had to download the <a
href="http://developers.facebook.com/resources.php">Client Library</a>. I choosed the Java Library since my development environment was Java. When I extracted the Client Library, only the source files where included. So what I had to do first (besides looking into the source code), was to compile the java-files. I actually wrote an ant-task for this purpose, but a more easier way is just to use javac without using ant-framework. The next step was to take the output from the compile process and put it into an archived file (.jar). This is not necessary but I like to do it this way and the facebook.jar file I now had is easily dropped into my existing web application library. In my Facebook profile I had to create (set up) a new application. This is done by clicking the developer application that was installed earlier. Its some steps including getting a key (API key and secret key) that I used in my java-code but this process is straight forward.</p><p
align="left">Ok, now lets start the coding process. Here is the java code controlling the session and authentication:</p><p
align="left"><em>String apiKey = &#8220;something&#8221;;<br
/> String secretKey = &#8220;something&#8221;; </em></p><p
align="left"><em>FacebookRestClient frc = null;<br
/> HttpSession session = request.getSession();<br
/> String sessionKey = (String) session.getAttribute(&#8221;facebookSession&#8221;);<br
/> String token = request.getParameter(&#8221;auth_token&#8221;);</em></p><p
align="left"><em>try {</em></p><p
align="left"><em>if (sessionKey != null &amp;&amp; sessionKey.length() &gt; 0) {</em></p><p
align="left"><em>frc = new FacebookRestClient(apiKey, secretKey, sessionKey);<br
/> this.doTheThing(request, response, frc);<br
/> } else if (token != null) {<br
/> frc = new FacebookRestClient(apiKey, secretKey);<br
/> session.setAttribute(&#8221;facebookSession&#8221;, sessionKey);<br
/> sessionKey = frc.auth_getSession(token);<br
/> session.setAttribute(&#8221;facebookSession&#8221;, sessionKey);<br
/> this.doTheThing(request, response, frc);<br
/> </em></p><p
align="left"><em>} else {<br
/> response.sendRedirect(&#8221;http://www.facebook.com/login.php?api_key=&#8221; + apiKey + &#8220;&amp;v=1.0&#8243;);<br
/> }<br
/> } catch (FacebookException fe) {<br
/> } catch (IOException ioe) {<br
/> &#8230;</em></p><p
align="left">The <em>doTheThing</em> method is extracting the information and looks like this:</p><p
align="left"><em>int myid = frc.users_getLoggedInUser();<br
/> EnumSet&lt;ProfileField&gt; fields = EnumSet.of(com.facebook.api.ProfileField.NAME, com.facebook.api.ProfileField.PIC, com.facebook.api.ProfileField.PIC_BIG, com.facebook.api.ProfileField.PIC_SMALL);<br
/> Collection&lt;Integer&gt; users = new ArrayList();<br
/> users.add(myid);</em></p><p><em>// Get my information<br
/> Document d = frc.users_getInfo(users, fields);<br
/> String myname = d.getElementsByTagName(&#8221;name&#8221;).item(0).getTextContent();<br
/> String mypicture = d.getElementsByTagName(&#8221;pic&#8221;).item(0).getTextContent();</em></p><p><em>// Get my friends id<br
/> Document d2 = frc.friends_get();<br
/> String s = d2.toString();<br
/> NodeList userIDNodes = d2.getElementsByTagName(&#8221;uid&#8221;);<br
/> int fcount = userIDNodes.getLength();</em></p><p><em>Collection&lt;Integer&gt; friends = new ArrayList&lt;Integer&gt;();<br
/> for (int i = 0; i &lt; fcount; i++) {<br
/> Node node = userIDNodes.item(i);<br
/> String idText = node.getTextContent();<br
/> Integer id = Integer.valueOf(idText);<br
/> friends.add(id);<br
/> }</em></p><p><em>List l = new ArrayList();<br
/> Map m = new HashMap();<br
/> Document d3 = frc.users_getInfo(friends, fields);</em></p><p>// Get my friends information<br
/> for (int j = 0; j &lt; fcount; j++) {<br
/> String name2 = d3.getElementsByTagName(&#8221;name&#8221;).item(j).getTextContent();<br
/> String picture2 = d3.getElementsByTagName(&#8221;pic&#8221;).item(j).getTextContent();<br
/> String picture3 = d3.getElementsByTagName(&#8221;pic_small&#8221;).item(j).getTextContent();<br
/> String picture4 = d3.getElementsByTagName(&#8221;pic_big&#8221;).item(j).getTextContent();</p><p><em>m.put(&#8221;name&#8221;, name2);<br
/> m.put(&#8221;picture&#8221;, picture2);<br
/> m.put(&#8221;picture_small&#8221;, picture3);<br
/> m.put(&#8221;picture_big&#8221;, picture4);</em></p><p><em>l .add(m);<br
/> m = new HashMap();</em></p><p><em>}<br
/> request.setAttribute(&#8221;friends&#8221;, l);<br
/> request.setAttribute(&#8221;myname&#8221;, myname);<br
/> request.setAttribute(&#8221;mypicture&#8221;, mypicture);<br
/> request.setAttribute(&#8221;numFriends&#8221;, new Integer(fcount));</em></p><p
align="left"><p
align="left">So all the information from Facebook is now added to the request and can be displayed in a JSP. The code above is not very elegant and is not optimized(!). Its just a pice of code done after half an hour of work and my goal was to implement an integration module for testing purpose.</p><p>Hope this blog post can help others looking for hints when getting started with Facebook application development/integration.</p> ]]></content:encoded> <wfw:commentRss>http://www.larre.com/2007/10/10/building-a-facebook-application-in-15-minutes/feed/</wfw:commentRss> <slash:comments>33</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk
Group:              tag
-->

<!-- W3 Total Cache: CDN debug info:
Engine:             cf
-->

<!-- W3 Total Cache: Db cache debug info:
Engine:             disk
Total queries:      13
Cached queries:     0
Total query time:   0.026
SQL info:
    # | Time (s) |    Caching (Reject reason)     |   Status   | Query
    1 |    0.015 |            enabled             | Not cached | SELECT option_name, option_value FROM wp_nj5ugn_options WHERE autoload = 'yes'
    2 |    0.001 |            enabled             | Not cached | SELECT option_value FROM wp_nj5ugn_options WHERE option_name = 'aiosp_post_title_format' LIMIT 1
    3 |    0.001 |            enabled             | Not cached | SELECT option_value FROM wp_nj5ugn_options WHERE option_name = 'openid_associations' LIMIT 1
    4 |    0.001 |            enabled             | Not cached | SELECT option_value FROM wp_nj5ugn_options WHERE option_name = 'openid_nonces' LIMIT 1
    5 |    0.001 |            enabled             | Not cached | SELECT post_modified_gmt FROM wp_nj5ugn_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1
    6 |    0.001 |            enabled             | Not cached | SELECT post_date_gmt FROM wp_nj5ugn_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1
    7 |    0.001 |            enabled             | Not cached | SELECT t.*, tt.* FROM wp_nj5ugn_terms AS t INNER JOIN wp_nj5ugn_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = 'post_tag' AND t.slug = 'code' LIMIT 1
    8 |    0.003 |  disabled (query is rejected)  | Not cached | SELECT SQL_CALC_FOUND_ROWS  wp_nj5ugn_posts.* FROM wp_nj5ugn_posts  INNER JOIN wp_nj5ugn_term_relationships ON (wp_nj5ugn_posts.ID = wp_nj5ugn_term_relationships.object_id) INNER JOIN wp_nj5ugn_term_taxonomy ON (wp_nj5ugn_term_relationships.term_taxonomy_id = wp_nj5ugn_term_taxonomy.term_taxonomy_id) INNER JOIN wp_nj5ugn_terms ON (wp_nj5ugn_term_taxonomy.term_id = wp_nj5ugn_terms.term_id)  WHERE 1=1  AND wp_nj5ugn_term_taxonomy.taxonomy = 'post_tag'  AND wp_nj5ugn_terms.slug IN ('code')  AND wp_nj5ugn_posts.post_type = 'post' AND (wp_nj5ugn_posts.post_status = 'publish') GROUP BY wp_nj5ugn_posts.ID ORDER BY wp_nj5ugn_posts.post_date DESC LIMIT 0, 10
    9 |    0.001 |  disabled (query is rejected)  | Not cached | SELECT FOUND_ROWS()
   10 |    0.001 |            enabled             | Not cached | SELECT t.*, tt.*, tr.object_id FROM wp_nj5ugn_terms AS t INNER JOIN wp_nj5ugn_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_nj5ugn_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag') AND tr.object_id IN (15) ORDER BY t.name ASC
   11 |    0.001 |            enabled             | Not cached | SELECT post_id, meta_key, meta_value FROM wp_nj5ugn_postmeta WHERE post_id IN (15)
   12 |    0.001 |            enabled             | Not cached | SELECT * FROM wp_nj5ugn_users WHERE ID = 2 LIMIT 1
   13 |    0.001 |            enabled             | Not cached | SELECT meta_key, meta_value FROM wp_nj5ugn_usermeta WHERE user_id = 2
-->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk
Key:                w3tc_99f03baef0ec9a5cf935256f33dc9796_page_da546bb50281a372f66e953b2ee4cd39
Caching:            disabled
Reject reason:      user agent is rejected
Status:             not cached
Creation Time:      0.332s
Header info:
X-Powered-By:       W3 Total Cache/0.8.5.1
Expires:            Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control:      no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma:             no-cache
X-Pingback:         http://www.larre.com/blog/xmlrpc.php
Last-Modified:      Fri, 29 Jan 2010 21:26:46 GMT
ETag:               "794e7f11c619555748c1fbcd485532dd"
Content-Type:       text/xml; charset=UTF-8
-->