Facebook dialog

最近的 case 要用到發布訊息到 facebook 的功能,因為簡單的 social plugin 只能按讚,需要加上一些特定的訊息就會需要用到 fb 的 api 了。


官方文件:Facebook Dialog

首先到 Facebook developers到新增一個 app ,並且記下APP ID。
Facebook 設定

APP 的這邊要設定要使用 API 的網址。
Facebook 設定

再來是照官方的文件貼上這段。
  1. <div id="fb-root"></div>   
  2. <script>   
  3.   window.fbAsyncInit = function() {   
  4.     // init the FB JS SDK   
  5.     FB.init({   
  6.       appId      : 'YOUR_APP_ID',                        // App ID from the app dashboard   
  7.       channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms   
  8.       status     : true,                                 // Check Facebook Login status   
  9.       xfbml      : true                                  // Look for social plugins on the page   
  10.     });   
  11.   
  12.     // Additional initialization code such as adding Event Listeners goes here   
  13.   };   
  14.   
  15.   // Load the SDK asynchronously   
  16.   (function(d, s, id){   
  17.      var js, fjs = d.getElementsByTagName(s)[0];   
  18.      if (d.getElementById(id)) {return;}   
  19.      js = d.createElement(s); js.id = id;   
  20.      js.src = "//connect.facebook.net/en_US/all.js";   
  21.      fjs.parentNode.insertBefore(js, fjs);   
  22.    }(document, 'script''facebook-jssdk'));   
  23. </script>  

官方文件是說 channelURL 是用來處理 cache 跟 x-domain 的,可以不填。

  1. FB.ui({   
  2.   method: 'feed',   
  3.   link: 'https://developers.facebook.com/docs/dialogs/',   
  4.   caption: 'An example caption',   
  5.   description: (   
  6.       'description here'  
  7.   ),   
  8.   picture: 'example.jpg'  
  9. }, function(response){});  

之後貼上這段,可以在各個選項填上值,然後再看要在哪個地方呼叫就可以了。

留言