LJC- Introduction to FB Integration

6,040
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
6,040
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Lots of different ways to do this – as we’re Java devs, I figured I’d stick to the server side authentication flow in Java
  • Lots of different ways to integrate – but as we are all java developers I figured I’d show the server authentication flow
  • HTTP sequence diagram
  • HTTP sequence diagram
  • LJC- Introduction to FB Integration

    1. 1. Being SocialA quick introduction to integrating with Facebook Kim Ross @kimbertles #ljcjug
    2. 2. Who am I? Kim Ross @kimbertles #ljcjug
    3. 3. Facebook Integration• http://developers.facebook.com/• Website• Mobile• Apps Kim Ross @kimbertles #ljcjug
    4. 4. http://apps.facebook.com/ljc-presentation Kim Ross @kimbertles #ljcjug
    5. 5. I, Robot
    6. 6. Kim Ross@kimbertles #ljcjug
    7. 7. Kim Ross @kimbertleshttp://developers.facebook.com/docs/authentication/server-side/ #ljcjug
    8. 8. FacebookController@RequestMapping("/fb/")public String fb( @RequestParam(value = "signed_request") String signedRequest, Model model)1. Check signed request signatureString[] parts = signedRequest.split(".", 2);Base64 decoder = new Base64(true); // Gotcha - decoder must be base64 URLString sig = new String(decoder.decode(parts[0].getBytes()));checkSignature(sig, parts[1]); String data = new String(decoder.decode(parts[1].getBytes())) Kim Ross @kimbertles #ljcjug
    9. 9. 2. Check if already authorised JSONObject sReq = new JSONObject(data); if (sReq.has("user_id")) { model.addAttribute("accessToken", sReq.getString("oauth_token")); return "facebook"; } Kim Ross @kimbertles #ljcjug
    10. 10. 3. If not authenticated, request permissionsStringBuilder redirectUrl = new StringBuilder( "http://www.facebook.com/dialog/oauth/?client_id=");redirectUrl.append(FacebookConstants.APP_ID);redirectUrl.append("&redirect_uri=");redirectUrl.append(URLEncoder.encode( "http://apps.facebook.com/ljc-presentation/auth/", "UTF-8"));redirectUrl.append("&state=");redirectUrl.append("MYSTATE");redirectUrl.append("&scope=");redirectUrl.append("email,user_likes");return "redirect:" + redirectUrl.toString(); Kim Ross @kimbertles #ljcjug
    11. 11. Kim Ross @kimbertleshttp://developers.facebook.com/docs/authentication/server-side/ #ljcjug
    12. 12. Getting the access tokenString code = request.getParameter("code");StringBuilder accessTokenUri = new StringBuilder( "https://graph.facebook.com/oauth/access_token?client_id=");accessTokenUri.append(FacebookConstants.APP_ID);accessTokenUri.append("&redirect_uri=");accessTokenUri.append(URLEncoder.encode( "http://apps.facebook.com/ljc-presentation/auth/", "UTF-8")); **accessTokenUri.append("&client_secret=");accessTokenUri.append(FacebookConstants.APP_SECRET);accessTokenUri.append("&code=");accessTokenUri.append(URLEncoder.encode(code, "UTF-8"));HttpClient client = new HttpClient();GetMethod getter = new GetMethod(accessTokenUri.toString());client.executeMethod(getter);String response = getter.getResponseBodyAsString();String accessToken = response.substring( response.indexOf("=") + 1, response.indexOf("&expires")); Kim Rossmodel.addAttribute("accessToken", accessToken); @kimbertles #ljcjug
    13. 13. FacebookController.fb@RequestMapping("/fb/auth/")public String auth(HttpServletRequest request, Model model)if (request.getParameter("error") != null) { // User denied app return "aww";} Kim Ross @kimbertles #ljcjug
    14. 14. Kim Ross @kimbertleshttp://developers.facebook.com/docs/authentication/server-side/ #ljcjug
    15. 15. The “Beautiful” app<a href="/ljc-facebook/me?accessToken=${accessToken}">Me</a><br><a href="/ljc-facebook/likes?accessToken=${accessToken}">Likes</a><br><a href="/ljc-facebook/friends?accessToken=${accessToken}">Friends</a> Kim Ross @kimbertles #ljcjug
    16. 16. InfoControllerprivate String facebookCall(String uri) throws HttpException, IOException { HttpClient client = new HttpClient(); GetMethod getter = new GetMethod(uri); client.executeMethod(getter); return getter.getResponseBodyAsString();}responseBody.jsp${response} Kim Ross @kimbertles #ljcjug
    17. 17. Me@RequestMapping("/me")public String me(@RequestParam accessToken, Model model) String body = facebookCall( "https://graph.facebook.com/me?access_token=" + accessToken); model.addAttribute("response", body); return "responseBody";}
    18. 18. Likes@RequestMapping("/likes")public String likes(@RequestParam accessToken, Model model) { String body = facebookCall( "https://graph.facebook.com/me/likes?access_token=" + accessToken); model.addAttribute("response", body); return "responseBody";}
    19. 19. Friends@RequestMapping("/friends")public String friends(@RequestParam accessToken, Model model) { String body = facebookCall( "https://graph.facebook.com/me/friends?access_token=" + accessToken); model.addAttribute("response", body); return "responseBody";}
    20. 20. Thanks for listening!http://developers.facebook.com/https://github.com/kross/ljc-facebook @kimbertles Kim Ross @kimbertles #ljcjug

    ×