SlideShare a Scribd company logo
+ Building Facebook Applications with Ruby Alex Koppel, 12/2010
WHY DEVELOP WITH FACEBOOK? Over 500 million users 50% of active users log on every day 150 million in USA 10 million in Germany Application Platform Over 550,000 applications Over 1,000,000 websites integrated with Facebook Over 150 million users use such FB integrations / month Huge audience, extensive Platform
WHAT CAN APPS OFFER USERS? Better interface to Facebook core functionality iPhoto, Wall moderation and publishing platforms Promotion of an existing service Netflix, Yelp, Goodreads Create a connection with fans of a brand Chase Community Giving, other branded apps Authentication Groupon, Kayak, Goodreads Many applications fall into multiple categories
WHAT KINDS OF APPS ARE THERE? Websites Your own site Complete control Javascript SDK Any level of integration Canvas Apps Tab Apps Mobile Apps Desktop Apps Hosted on facebook.com Less friction for users on the site Some limitations (size/etc.) FBML+FBJS => restrictions
WEBSITES Wherever, whatever Benefits for users A way to share activity Easy login Lightweight integration Social plugins (iframes) Javascript SDK only Heavyweight integration Javascript frontend Server backend
CANVAS APPS Live on Facebook iframe or FBML iframe ~ website FBML to be retired Less friction than off-FB apps Users stay on site Have to work within Facebook limits iframe container Fixed size
TAB APPS Live on Facebook Page Tight integration with Page (brand/product) Can help drive Liking Limitations FBML/FBJS only  (iframe in the future) Size limitation First load is anonymous
FACEBOOK INTEGRATION Both front- and back-end integration Browser handles tasks requiring interaction Login, permissions, publishing stories Server handles many API-related tasks Offline access, realtime updates, app management Persisting results Many tasks can be done by either server or client Authentication API calls  Let your need guide you
ACCESSING FACEBOOK
FACEBOOK AUTHENTICATION OAuth-based authentication scheme Token provides access to user info Multiple ways to authenticate Javascript API Redirect with OAuth code Signed request parameters Provided in request to tab and canvas apps Old proprietary auth scheme being deprecated
AUTHENTICATING WITH JAVASCRIPT iframe-based secure login prompt XFBML login button Programatic via Javascript Includes permissions if desired Browser gets OAuth token, user ID, expiration, etc.  Notified through callback and event Javascript sets signed cookie cookies Server libraries provided parsing Preferred method Easy for users Keeps people on your site
AUTHENTICATING WITH REDIRECTS Send user to login page on facebook.com User redirected to app’s callback with OAuth code App server requests token using code and secret Server gets OAuth token and expiration Can then make server-side API calls Use the JS login if you can Redirects are disruptive Requires server-server communication
PERMISSIONS Granular permission model 55 different controls over data access User data Feed, likes, checkins Demographic data, email Friends’ data Friends’ demographics, likes, checkins, etc. Cannot get friends of friends Acting for user Publish to feed stories
FACEBOOK APIS Facebook has 2.5 server-side APIs All RESTful with JSON responses Graph API New, fast, shiny, incomplete OAuth authentication Accessible via the browser REST API Supports almost the entire API To be deprecated, someday Fast new OAuth endpoint Slow old custom auth endpoint
API METHODS REST API method names Many but not all also available via Graph API events.invite pages.blockFan fql.query photos.upload, videos.upload ads.createCampaigns status.set admin.setAppProperties Graph API only: https://graph.facebook.com/user_id/checkins https://graph.facebook.com/app_id/insights
FACEBOOK AND RUBY
MAKING IT WORK WITH RUBY Many Facebook libraries for new Graph/REST APIs Koala Mogli MiniFB rest-graph FBGraph For old REST API/custom auth, one clear winner Facebooker
KOALA My goals: lightweight, fast, flexible, tested All results as basic Ruby objects (Hash, Array)* Full support for Facebook platform OAuth, Graph, REST, realtime updates, test users  Typhoeus and Net::HTTP built in Full RSpec coverage Rails 3- and Ruby 1.9.2-compatible * get_connection and search return GraphCollection < Array, which provides pagination support
AUTHENTICATION WITH COOKIES @oauth = Koala::Facebook::OAuth.new(app_id, secret, callback_url) # pass in Rack cookies directly @oauth.get_user_from_cookies(cookies)  # => &quot;2905623&quot; @oauth.get_user_info_from_cookies(cookies)  # => { &quot;session_key&quot;=> ...,  &quot;uid&quot;=>&quot;2905623&quot; ,  &quot;sig&quot;=> ...,  &quot;secret&quot;=>&quot;YeOkUZhI6csLggJ_jFmm2A__&quot;,  &quot;access_token&quot; => ... }
AUTHENTICATION WITH REDIRECTS @oauth = Koala::Facebook::OAuth.new(app_id, secret, callback_url) @oauth.url_for_oauth_code(:permissions => &quot;publish_stream&quot;)  # => url # send your users there, they’ll return with their codes @oauth.get_access_token(code) # => token @oauth.get_access_token_info(code) # => { &quot;expires&quot; => seconds_from_now,  &quot;access_token&quot; => ... }
USING THE GRAPH API @graph = Koala::Facebook::GraphAPI.new(oauth_token) @graph.get_object(&quot;koppel&quot;)  # => { &quot; name &quot;=>&quot;Alex Koppel&quot;,  &quot; timezone &quot;=>1,  &quot; gender &quot;=>&quot;male&quot;,  &quot; id &quot;=>&quot;2905623&quot;,  &quot; first_name &quot;=>&quot;Alex&quot;, &quot; last_name &quot;=>&quot;Koppel&quot;,  &quot; updated_time &quot;=>&quot;2009-10-25T21:27:40+0000&quot;,  &quot; verified &quot;=>true,  &quot; locale &quot;=>&quot;de_DE&quot;,  &quot; link &quot;=>&quot;http://www.facebook.com/koppel&quot; }
USING THE GRAPH API  @graph.get_connections(&quot;koppel&quot;, &quot;feed&quot;)  # => [{  # lots of data! &quot; id &quot;=>&quot;2905623_174424332576660&quot;,  &quot; message &quot;=>&quot;At Can-Tine drinking beer, eating spicy poutine, and watching Army of Darkness.&quot;,  &quot; likes &quot;=>2, &quot; comments &quot;=>{&quot;data&quot;=>[{&quot;from&quot;=>{&quot;name&quot;=>&quot;Isaac Wolkerstorfer&quot;, &quot;id&quot;=>&quot;2900100&quot;}, &quot;id&quot;=>&quot;2905623_174424332576660_2109285&quot;, &quot;created_time&quot;=>&quot;2010-12-01T20:31:44+0000&quot;, &quot;message&quot;=>&quot;This place sounds amazing.&quot;}], &quot;count&quot;=>1} &quot; from &quot;=>{&quot;name&quot;=>&quot;Alex Koppel&quot;, &quot;id&quot;=>&quot;2905623&quot;}, &quot; created_time &quot;=>&quot;2010-12-01T18:05:09+0000&quot;,  &quot; type &quot;=>&quot;status&quot;,  “ privacy &quot;=>{&quot;value&quot;=>&quot;NETWORKS_FRIENDS&quot;},  &quot; attribution &quot;=>&quot;iPhone&quot;, &quot; actions &quot;=>[{&quot;name&quot;=>&quot;Comment&quot;, &quot;link&quot;=>...}, ...],  }, ...]
USING THE GRAPH API  @graph.search(&quot;Muenchen&quot;)  # => [{  # lots of data! {&quot; from &quot;=>{&quot;name&quot;=>&quot;Daniel Ragogna&quot;, &quot;id&quot;=>&quot;100000110972279&quot;}, &quot; type &quot; => &quot;status&quot;, &quot; message &quot;=>&quot;Muenchen + Dom Rep. Wir kommen Juhu&quot;, &quot; id &quot;=>&quot;100000110972279_160648203978214&quot;},  {&quot; from &quot;=>{&quot;name&quot;=>&quot;Scheinen Saint Jesus Jong&quot;, &quot;id&quot;=>&quot;561597582&quot;}, &quot; type &quot; => &quot;status&quot;, &quot; message &quot;=>&quot;muenchen i m comming&quot;, &quot;id &quot; =>&quot;561597582_171405789546392&quot;},  {&quot; name &quot;=>&quot;Main Concept - Muenchen 58&quot;, &quot; from &quot;=>{&quot;name&quot;=>&quot;Sergej Galkin&quot;, &quot;id&quot;=>&quot;100001607331988&quot;}, &quot; id &quot;=>&quot;100001607331988_154183774628283&quot;, &quot; type &quot;=>&quot;video&quot;,  &quot; source &quot;=>&quot;http://www.youtube.com/v/tyXYK-CgBQo&autoplay=1&quot;, &quot; message &quot;=>&quot;...das Spiel Das Ich Treib...&quot;}, ...]
USING THE GRAPH API @graph.put_wall_post(&quot; hey, i'm learning koala &quot;) # => {&quot;id&quot;=> new_post_id) @graph.put_comment(new_post_id, comment_text)  # => {&quot;id&quot;=> new_comment_id} @graph.delete_object(new_comment_id) # => true
INTEGRATING KOALA Easy to integrate Koala into Rails Create a before_filter for OAuth validation GraphCollections provide pagination params for urls Documentation on github wiki Can also integrate into authentication middleware Devise, OmniAuth, etc.
JAVASCRIPT SDK (FORMERLY FACEBOOK CONNECT)
JAVASCRIPT SDK OVERVIEW Evented Javascript library for Facebook interaction Login and permission management Cookie session storage XFBML Like and login/logout buttons, profile pics and names Asynchronous API access Avoid server load/delays when generating your UI
JAVASCRIPT SDK EXAMPLES Login with permissions FB.login(callback, {perms: permList}) FB.Event.subscribe(&quot;auth.statusChange&quot;, callback); function callback(response) { if (response.session) { // user successfully logged in } else {   // user cancelled login } }; XFBML login button: <fb:login-button perms=“read_stream&quot;></fb:login-button>
JAVASCRIPT SDK EXAMPLES Is the user a fan of your Page? FB.api({method: &quot;pages.isFan”, page_id: pid}, callback); // can show/hide UI elements based on the result Friend list FB.api({method: ”friends.get”}, callback); // build the UI based on the response
WRAPUP
RESOURCES Facebook Developer Wiki Graph API Javascript SDK REST API Facebook Developer Policies Facebook’s Best Practices Facebook’s Bugzilla Koala Facebook library OAuth Playground
ME http://facebook.com/koppel   https://graph.facebook.com/koppel   http://twitter.com/arsduo http://github.com/arsduo http://blog.twoalex.com
EXTRAS
APPENDIX: GRAPH API IN THE BROWSER! You can play with the Graph API in any browser https://graph.facebook.com/ID/CONNECTION_TYPE https://graph.facebook.com/koppel https://graph.facebook.com/contextoptional/feed Private info requires access_token parameter Returns JSON to your browser Go to  http://graph.facebook.com/  to authenticate Links with your OAuth token are halfway down Introduction Add metadata=1 to object URLs to see available options Great way to explore the graph
APPENDIX: PUT_WALL_POST In general, don’t post stories via the API Policy limits what you can post automatically Can’t include user message Browser prompt is a much better experience API use appropriate for offline/mobile experiences

More Related Content

Facebook + Ruby

  • 1. + Building Facebook Applications with Ruby Alex Koppel, 12/2010
  • 2. WHY DEVELOP WITH FACEBOOK? Over 500 million users 50% of active users log on every day 150 million in USA 10 million in Germany Application Platform Over 550,000 applications Over 1,000,000 websites integrated with Facebook Over 150 million users use such FB integrations / month Huge audience, extensive Platform
  • 3. WHAT CAN APPS OFFER USERS? Better interface to Facebook core functionality iPhoto, Wall moderation and publishing platforms Promotion of an existing service Netflix, Yelp, Goodreads Create a connection with fans of a brand Chase Community Giving, other branded apps Authentication Groupon, Kayak, Goodreads Many applications fall into multiple categories
  • 4. WHAT KINDS OF APPS ARE THERE? Websites Your own site Complete control Javascript SDK Any level of integration Canvas Apps Tab Apps Mobile Apps Desktop Apps Hosted on facebook.com Less friction for users on the site Some limitations (size/etc.) FBML+FBJS => restrictions
  • 5. WEBSITES Wherever, whatever Benefits for users A way to share activity Easy login Lightweight integration Social plugins (iframes) Javascript SDK only Heavyweight integration Javascript frontend Server backend
  • 6. CANVAS APPS Live on Facebook iframe or FBML iframe ~ website FBML to be retired Less friction than off-FB apps Users stay on site Have to work within Facebook limits iframe container Fixed size
  • 7. TAB APPS Live on Facebook Page Tight integration with Page (brand/product) Can help drive Liking Limitations FBML/FBJS only (iframe in the future) Size limitation First load is anonymous
  • 8. FACEBOOK INTEGRATION Both front- and back-end integration Browser handles tasks requiring interaction Login, permissions, publishing stories Server handles many API-related tasks Offline access, realtime updates, app management Persisting results Many tasks can be done by either server or client Authentication API calls Let your need guide you
  • 10. FACEBOOK AUTHENTICATION OAuth-based authentication scheme Token provides access to user info Multiple ways to authenticate Javascript API Redirect with OAuth code Signed request parameters Provided in request to tab and canvas apps Old proprietary auth scheme being deprecated
  • 11. AUTHENTICATING WITH JAVASCRIPT iframe-based secure login prompt XFBML login button Programatic via Javascript Includes permissions if desired Browser gets OAuth token, user ID, expiration, etc. Notified through callback and event Javascript sets signed cookie cookies Server libraries provided parsing Preferred method Easy for users Keeps people on your site
  • 12. AUTHENTICATING WITH REDIRECTS Send user to login page on facebook.com User redirected to app’s callback with OAuth code App server requests token using code and secret Server gets OAuth token and expiration Can then make server-side API calls Use the JS login if you can Redirects are disruptive Requires server-server communication
  • 13. PERMISSIONS Granular permission model 55 different controls over data access User data Feed, likes, checkins Demographic data, email Friends’ data Friends’ demographics, likes, checkins, etc. Cannot get friends of friends Acting for user Publish to feed stories
  • 14. FACEBOOK APIS Facebook has 2.5 server-side APIs All RESTful with JSON responses Graph API New, fast, shiny, incomplete OAuth authentication Accessible via the browser REST API Supports almost the entire API To be deprecated, someday Fast new OAuth endpoint Slow old custom auth endpoint
  • 15. API METHODS REST API method names Many but not all also available via Graph API events.invite pages.blockFan fql.query photos.upload, videos.upload ads.createCampaigns status.set admin.setAppProperties Graph API only: https://graph.facebook.com/user_id/checkins https://graph.facebook.com/app_id/insights
  • 17. MAKING IT WORK WITH RUBY Many Facebook libraries for new Graph/REST APIs Koala Mogli MiniFB rest-graph FBGraph For old REST API/custom auth, one clear winner Facebooker
  • 18. KOALA My goals: lightweight, fast, flexible, tested All results as basic Ruby objects (Hash, Array)* Full support for Facebook platform OAuth, Graph, REST, realtime updates, test users Typhoeus and Net::HTTP built in Full RSpec coverage Rails 3- and Ruby 1.9.2-compatible * get_connection and search return GraphCollection < Array, which provides pagination support
  • 19. AUTHENTICATION WITH COOKIES @oauth = Koala::Facebook::OAuth.new(app_id, secret, callback_url) # pass in Rack cookies directly @oauth.get_user_from_cookies(cookies) # => &quot;2905623&quot; @oauth.get_user_info_from_cookies(cookies) # => { &quot;session_key&quot;=> ..., &quot;uid&quot;=>&quot;2905623&quot; , &quot;sig&quot;=> ..., &quot;secret&quot;=>&quot;YeOkUZhI6csLggJ_jFmm2A__&quot;, &quot;access_token&quot; => ... }
  • 20. AUTHENTICATION WITH REDIRECTS @oauth = Koala::Facebook::OAuth.new(app_id, secret, callback_url) @oauth.url_for_oauth_code(:permissions => &quot;publish_stream&quot;) # => url # send your users there, they’ll return with their codes @oauth.get_access_token(code) # => token @oauth.get_access_token_info(code) # => { &quot;expires&quot; => seconds_from_now, &quot;access_token&quot; => ... }
  • 21. USING THE GRAPH API @graph = Koala::Facebook::GraphAPI.new(oauth_token) @graph.get_object(&quot;koppel&quot;) # => { &quot; name &quot;=>&quot;Alex Koppel&quot;, &quot; timezone &quot;=>1, &quot; gender &quot;=>&quot;male&quot;, &quot; id &quot;=>&quot;2905623&quot;, &quot; first_name &quot;=>&quot;Alex&quot;, &quot; last_name &quot;=>&quot;Koppel&quot;, &quot; updated_time &quot;=>&quot;2009-10-25T21:27:40+0000&quot;, &quot; verified &quot;=>true, &quot; locale &quot;=>&quot;de_DE&quot;, &quot; link &quot;=>&quot;http://www.facebook.com/koppel&quot; }
  • 22. USING THE GRAPH API @graph.get_connections(&quot;koppel&quot;, &quot;feed&quot;) # => [{ # lots of data! &quot; id &quot;=>&quot;2905623_174424332576660&quot;, &quot; message &quot;=>&quot;At Can-Tine drinking beer, eating spicy poutine, and watching Army of Darkness.&quot;, &quot; likes &quot;=>2, &quot; comments &quot;=>{&quot;data&quot;=>[{&quot;from&quot;=>{&quot;name&quot;=>&quot;Isaac Wolkerstorfer&quot;, &quot;id&quot;=>&quot;2900100&quot;}, &quot;id&quot;=>&quot;2905623_174424332576660_2109285&quot;, &quot;created_time&quot;=>&quot;2010-12-01T20:31:44+0000&quot;, &quot;message&quot;=>&quot;This place sounds amazing.&quot;}], &quot;count&quot;=>1} &quot; from &quot;=>{&quot;name&quot;=>&quot;Alex Koppel&quot;, &quot;id&quot;=>&quot;2905623&quot;}, &quot; created_time &quot;=>&quot;2010-12-01T18:05:09+0000&quot;, &quot; type &quot;=>&quot;status&quot;, “ privacy &quot;=>{&quot;value&quot;=>&quot;NETWORKS_FRIENDS&quot;}, &quot; attribution &quot;=>&quot;iPhone&quot;, &quot; actions &quot;=>[{&quot;name&quot;=>&quot;Comment&quot;, &quot;link&quot;=>...}, ...], }, ...]
  • 23. USING THE GRAPH API @graph.search(&quot;Muenchen&quot;) # => [{ # lots of data! {&quot; from &quot;=>{&quot;name&quot;=>&quot;Daniel Ragogna&quot;, &quot;id&quot;=>&quot;100000110972279&quot;}, &quot; type &quot; => &quot;status&quot;, &quot; message &quot;=>&quot;Muenchen + Dom Rep. Wir kommen Juhu&quot;, &quot; id &quot;=>&quot;100000110972279_160648203978214&quot;}, {&quot; from &quot;=>{&quot;name&quot;=>&quot;Scheinen Saint Jesus Jong&quot;, &quot;id&quot;=>&quot;561597582&quot;}, &quot; type &quot; => &quot;status&quot;, &quot; message &quot;=>&quot;muenchen i m comming&quot;, &quot;id &quot; =>&quot;561597582_171405789546392&quot;}, {&quot; name &quot;=>&quot;Main Concept - Muenchen 58&quot;, &quot; from &quot;=>{&quot;name&quot;=>&quot;Sergej Galkin&quot;, &quot;id&quot;=>&quot;100001607331988&quot;}, &quot; id &quot;=>&quot;100001607331988_154183774628283&quot;, &quot; type &quot;=>&quot;video&quot;, &quot; source &quot;=>&quot;http://www.youtube.com/v/tyXYK-CgBQo&autoplay=1&quot;, &quot; message &quot;=>&quot;...das Spiel Das Ich Treib...&quot;}, ...]
  • 24. USING THE GRAPH API @graph.put_wall_post(&quot; hey, i'm learning koala &quot;) # => {&quot;id&quot;=> new_post_id) @graph.put_comment(new_post_id, comment_text) # => {&quot;id&quot;=> new_comment_id} @graph.delete_object(new_comment_id) # => true
  • 25. INTEGRATING KOALA Easy to integrate Koala into Rails Create a before_filter for OAuth validation GraphCollections provide pagination params for urls Documentation on github wiki Can also integrate into authentication middleware Devise, OmniAuth, etc.
  • 26. JAVASCRIPT SDK (FORMERLY FACEBOOK CONNECT)
  • 27. JAVASCRIPT SDK OVERVIEW Evented Javascript library for Facebook interaction Login and permission management Cookie session storage XFBML Like and login/logout buttons, profile pics and names Asynchronous API access Avoid server load/delays when generating your UI
  • 28. JAVASCRIPT SDK EXAMPLES Login with permissions FB.login(callback, {perms: permList}) FB.Event.subscribe(&quot;auth.statusChange&quot;, callback); function callback(response) { if (response.session) { // user successfully logged in } else { // user cancelled login } }; XFBML login button: <fb:login-button perms=“read_stream&quot;></fb:login-button>
  • 29. JAVASCRIPT SDK EXAMPLES Is the user a fan of your Page? FB.api({method: &quot;pages.isFan”, page_id: pid}, callback); // can show/hide UI elements based on the result Friend list FB.api({method: ”friends.get”}, callback); // build the UI based on the response
  • 31. RESOURCES Facebook Developer Wiki Graph API Javascript SDK REST API Facebook Developer Policies Facebook’s Best Practices Facebook’s Bugzilla Koala Facebook library OAuth Playground
  • 32. ME http://facebook.com/koppel https://graph.facebook.com/koppel http://twitter.com/arsduo http://github.com/arsduo http://blog.twoalex.com
  • 34. APPENDIX: GRAPH API IN THE BROWSER! You can play with the Graph API in any browser https://graph.facebook.com/ID/CONNECTION_TYPE https://graph.facebook.com/koppel https://graph.facebook.com/contextoptional/feed Private info requires access_token parameter Returns JSON to your browser Go to http://graph.facebook.com/ to authenticate Links with your OAuth token are halfway down Introduction Add metadata=1 to object URLs to see available options Great way to explore the graph
  • 35. APPENDIX: PUT_WALL_POST In general, don’t post stories via the API Policy limits what you can post automatically Can’t include user message Browser prompt is a much better experience API use appropriate for offline/mobile experiences