以åã¯Facebookãçºä¿¡ãã¦ããRSS/XMLå½¢å¼ã®feedæ
å ±ãåå¾ããæ¯è¼çç°¡åã«èªèº«ã®WEBãµã¤ãçã«Facebookãã¼ã¸ã®æ稿ãã¼ã¿ãªã©ã表示ãããäºãåºæ¥ã¾ããããFacebookã®feedé
ä¿¡ã®æ©è½ã2015å¹´6æã«ä»æ§å¤æ´ãã¦ãã¾ããç¾å¨ã¯JSON ï¼JavaScript Object Notationï¼å½¢å¼ã®ãã£ã¼ãæ
å ±ãGraph APIã使ããã¼ã¿ãåå¾ããã¨ããé常ã«ãããããä»æ§ã«ãªã£ã¦ãã¾ãã¾ããã
ãã¡ãããFacebookå´ãç¨æãã¦ãããPage Pluginãã使ãã°ç°¡åã«èªèº«ã®WEBãµã¤ãçã«Facebookãã¼ã¸ã®æ稿ãã¼ã¿ãªã©ã表示ãããäºã¯åºæ¥ã¾ããããPage Pluginãã使ããã«ä»åã®ããã«ãã£ã¼ãæ
å ±ã使ããã¨ã§ã
- ããã¹ããã¼ã¿ã§åºåãããçºãGoogleã®ã¯ãã¼ã©ã¼ãèªèãããããªãã
- ããã¹ããã¼ã¿ãªã®ã§CSSã§èªç±ã«ä¿®é£¾ãåºæ¥ãã
- ãPage Pluginãã使ããªãäºã§ãã¼ã¸ã®è¡¨ç¤ºãæ©ãããäºãåºæ¥ãã
çã®ã¡ãªãããããã¾ãã
JSONã¨ã¯ï¼
JSONï¼ã¸ã§ã¤ã½ã³ãJavaScript Object Notationï¼ã¯è»½éãªãã¼ã¿è¨è¿°è¨èªã®1ã¤ã§ãããæ§æã¯JavaScriptã«ããããªãã¸ã§ã¯ãã®è¡¨è¨æ³ããã¼ã¹ã¨ãã¦ããããJSONã¯JavaScriptå°ç¨ã®ãã¼ã¿å½¢å¼ã§ã¯æ±ºãã¦ãªããæ§ã
ãªã½ããã¦ã§ã¢ãããã°ã©ãã³ã°è¨èªéã«ããããã¼ã¿ã®åã渡ãã«ä½¿ããããè¨è¨ããã¦ããã
GraphAPIã使ã£ã¦Facebookãã¼ã¸ã®æ å ±ãåå¾
AppIDã®åå¾
ã¾ãã¯https://developers.facebook.com/ã®ãMy Appsãããæ°ããã¢ããªã®ç»é²ããã¦AppIDçãåå¾ãã¾ãã
ãã¼ã¯ã³ã®åå¾
次ã«https://developers.facebook.com/tools/accesstoken/ã§ãã¼ã¯ã³ã®åå¾ãè¡ãã¾ãã
ããã«æ稿ãã¼ã¿ãåå¾ããèªèº«ã®ãµã¤ãã®è¡¨ç¤ºãããå ´åã¯App Tokenã®ã¿ã§å¤§ä¸å¤«ã§ããããªã³ã¯ãä»ã®æ©è½ã使ãããå ´åã¯User Tokenã使ããAccess Tokenããåå¾ããå¿
è¦ãããã¾ãã
ãApp Tokenãã®å ´åãç¹ã«æå¹æéã¯æãã¾ãããããAccess Tokenãã®å ´åã¯æ大ã§ãæå¹æéãï¼ã¶æãªã®ã§æ³¨æãå¿
è¦ã§ãã
ãAccess Tokenãã®åå¾
次ã«Graph API Explorerhttps://developers.facebook.com/tools/explorer/
ã«ã¢ã¯ã»ã¹ãã¦ããAccess Tokenããåå¾ãã¾ãã
ç¨éã«ãã£ã¦ãå
ã»ã©åå¾ããApp Tokenãããã¯User TokenããAccess Tokenãã®æ ã®ä¸ã«æ¿å
¥ãããã®ä¸ã®ã/v2.3/ã以éã«ãã¼ã¸åãå
¥åãã¦submitãã¯ãªãã¯ããã¨Facebookãã¼ã¸ã®æ
å ±ãJSONã§è¡¨ç¤ºããã¾ããããã§è¡¨ç¤ºãããJSONãã¼ã¿ã®ä¸ã®ãFacebookãã¼ã¸IDããå¾ã»ã©å¿
è¦ã«ãªãã¾ãã
HTMLã§åºå
ä»ã¾ã§ã®å·¥ç¨ã§åå¾ãããAccess Tokenããããã¯ãApp Tokenãã¨ãFacebookãã¼ã¸IDããURLã«å½ã¦ã¯ããã¢ã¯ã»ã¹ããã¨ãã£ã¼ããJSONå½¢å¼ã§åç §ã§ãã¾ãã
[https://graph.facebook.com/v2.3/ãFacebookãã¼ã¸IDã/feed?access_token=ãAccess Tokenãããã¯App Tokenã]
JSONã¯JavaScriptãã¼ã¹ã§è¨è¨ããã¦ã¯ãã¾ãããPHPãªã©ä»ã®è¨èªã§ããã¼ã¿ã®å¼ã渡ããåºæ¥ãã®ãç¹å¾´ã§ãã
ä»åã¯jQueryã®ã.getJSONãã¡ã½ããã使ãå¿
è¦ãªãã¼ã¿ãå¼ã£å¼µã£ã¦æ¥ã¦ãliã¿ã°ãªã©ã«çæããCSSã§é©å½ã«èªã¿ãããããã°å®äºã
HTMLãµã³ãã«ã½ã¼ã¹
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8"> <title>Facebookãã¼ã¸ã®ãã£ã¼ãæ å ±ãåå¾ããï¼JSONå½¢å¼ï¼</title> <style> li { list-style: none; margin-bottom: 20px; width: 500px; } span { margin-right: 20px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(function(){ $.ajax({ type: "GET", url: "https://graph.facebook.com/ãFacebookãã¼ã¸IDã/feed?access_token=ãAccess Tokenãããã¯App Tokenã", dataType: "json", success: function(json){ json = json.data; var num = json.length; var Feed = "<ul>\n"; for(var i=0; i<=4; i++){ var time = parseISO8601(json[i].created_time); var text = json[i].message; var fblink = json[i].link; var fbphoto = json[i].picture; Feed += '<li><span>' + time + '</span>' +'<br>' + '<a href="' + fblink + '">' +text+ '</a>' + '<p><img src="' +fbphoto+ '"></p>' + '</li>\n'; } Feed += "</ul>\n"; $("#topics").append(Feed); } }); }); function parseISO8601(str) { var date = str.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/); return date = date[1] + "å¹´"+ date[2] + "æ" + date[3] + "æ¥"; } </script> </head> <body> <h1>Facebookãã¼ã¸ ãã£ã¼ã 表示</h1> <div id="topics"></div> </body> </html>
ä»åã®ãµã³ãã«ã®ããã«è¨äºãã¼ã¿ã¨ç»åãã¼ã¿ã表示ãããå ´åã¯ãAccess Tokenããå¿
è¦ã«ãªãã¾ããä¸ã§ãæ¸ããããã«ãAccess Tokenãã¯æå¹æéãæ大ï¼ã¶æã§ãæå¹æéãéããã¨ãã£ã¼ãæ
å ±ã表示ãããªããªã£ã¦ãã¾ãã®ã§ãæä½ã§ãï¼ã¶æã«ï¼åº¦ã¯ãµã¤ãã®ã¡ã³ããã³ã¹ãå¿
è¦ã§ãã
ãããç¡çãªå ´åã¯ãApp Tokenãã使ã£ã¦ããã¹ããã¼ã¿ã®ã¿ã表示ãããããç´ ç´ã«Facebookãç¨æãã¦ãããPage Pluginãã使ãã¾ãããã
åèãµã¤ã