墙贴问题?

时间:2012-03-06 06:28:29

标签: javascript facebook

我试图从我的fb应用程序制作wallpost。我的代码(吼叫)不会给eny erros,但它不会发布任何东西到墙上。有人能帮我吗? = P

应用程序文件(index.php):

<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
<body>

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '1234567890', // App ID
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    // Additional initialization code here
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));
</script>



<a href="#" onClick="postToFacebook()">Post to Facebook</a>

<script>
function postToFacebook() {
    var body = 'Reading Connect JS documentation';

    FB.api('/me/feed', 'post', { body: body, message: 'My message is ...' }, function(response) {
      if (!response || response.error) {
        alert('Error occured');
      } else {
        alert('Post ID: ' + response);
      }
    });
}
</script>

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId  : 'YOUR APP ID GOES HERE',
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });
  };

  (function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

</body>
</html>

服务器中的通道文件(channel.php):

 <?php
 $cache_expire = 60*60*24*365;
 header("Pragma: public");
 header("Cache-Control: max-age=".$cache_expire);
 header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
 ?>
 <script src="//connect.facebook.net/en_US/all.js"></script>

1 个答案:

答案 0 :(得分:1)

为什么要尝试两次启动Facebook脚本?:) 这是一个工作代码的示例: How to get FB.api('/me/feed', 'post', ... to work? 请注意,在您进行FB.api调用之前,用户需要首先授权应用程序。 你可以使用FB.login来做到这一点,例如: http://developers.facebook.com/docs/reference/javascript/FB.login/ 并使用FBgetLoginStatus()检查用户是否实际连接到您的应用程序: http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ 希望这有帮助