Javascript Fb.api仅在登录时发布到用户墙

Javascript Fb.api仅在登录时发布到用户墙,javascript,facebook,Javascript,Facebook,我想使用fb.api发布登录用户,但只发布一次。如果我把这个 var params = {}; params['message'] = 'gegeegeggegall! Check out www.facebook.com/trashcandyrock for more info.'; params['name'] = 'gegeggeeg - gegegege'; params['description'] = 'Check out Tegegegeg! Win merch by playin

我想使用fb.api发布登录用户,但只发布一次。如果我把这个

var params = {};
params['message'] = 'gegeegeggegall! Check out www.facebook.com/trashcandyrock for more info.';
params['name'] = 'gegeggeeg - gegegege';
params['description'] = 'Check out Tegegegeg! Win merch by playing and reccomending to your friends.';
params['link'] = 'http://www.bblblba.com';
params['picture'] = 'http://summer-mourning.zoocha.com/uploads/thumb.png';
params['caption'] = 'Tgegegegeeg';

FB.api('/me/feed', 'post', params, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Published to stream - you might want to delete it now!');
  }
});
每次用户刷新站点时,它都会发布到墙上


怎么办?

触发FB.api调用的是什么?如果它只是标记中的代码,那么它将在浏览器到达该点时立即运行


您可能会在FB.api调用后存储某种cookie值或其他内容,然后在页面加载时检查它,但这似乎比可能需要的工作量要多。

您希望他只发布一次吗

如果是这样,您将需要创建一个“状态”。为了做到这一点,您可以在客户端(使用cookie)或服务器端(使用数据库)进行操作

创建一个名为“posted”的布尔变量,并将其存储在cookie或数据库中(因为您使用的是javascript,所以使用cookie可能更容易)


setCookie和getCookie的定义:

您可以运行FQL查询,并通过使用应用程序id查询流表来检查消息是否已发布。类似于:

<!DOCTYPE html>
<html>
<body>
<div id="fb-root"></div>
<a href="#" onclick="postToWall();return false;">Post To Wall</a>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({ appId  : '**yourAppID**', status : true, cookie : true, xfbml  : true });

  function postToWall() {  
    FB.login(function(response) {
      if (response.session) {
        FB.api(
          {
            method: 'fql.query',
            query: 'SELECT post_id, message from stream where app_id = **yourAppID** and source_id = me()'
          },
          function(response) {
            if(response.length == 0){
              FB.ui(
              {
                 method: 'feed',
                 name: 'Facebook Dialogs',
                 link: 'https://developers.facebook.com/docs/reference/dialogs/',
                 picture: 'http://fbrell.com/f8.jpg',
                 caption: 'Reference Documentation',
                 description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
                 message: 'Facebook Dialogs are easy!'
              },
              function(response) {
                if (response && response.post_id) {
                   alert('Post was published.');
                } else {
                  alert('Post was not published.');
                }
              }
             );         
            }
            else { 
              alert('User already posted this message');
            }
          }
        );
      }
    } , {perms:''}); 
}
</script>
</body>
</html>

init({appId:'**yourAppID**',状态:true,cookie:true,xfbml:true});
函数postToWall(){
FB.登录(功能(响应){
if(response.session){
FB.api(
{
方法:“fql.query”,
查询:“选择post\u id,来自流的消息,其中app\u id=**yourAppID**和source\u id=me()”
},
功能(响应){
if(response.length==0){
FB.ui(
{
方法:“提要”,
名称:“Facebook对话”,
链接:'https://developers.facebook.com/docs/reference/dialogs/',
图片:'http://fbrell.com/f8.jpg',
标题:“参考文档”,
描述:“对话框为应用程序与用户交互提供了一个简单、一致的界面。”,
留言:“Facebook对话很简单!”
},
功能(响应){
if(应答和应答后id){
警报(“发布帖子”);
}否则{
警报(“未发布帖子”);
}
}
);         
}
否则{
警报(“用户已发布此消息”);
}
}
);
}
},{perms:'});
}

我想看看是否可以在没有cookie的情况下完成?比如说,我需要在用户点击按钮允许我的应用程序在他的墙上发布的确切时刻发布在用户的墙上。所以我想在他接受我的比赛并第一次比赛时贴在他的墙上。我是不是遗漏了什么?我应该换一种方式吗?在这种情况下,您必须向该按钮添加一个事件来执行代码(作为函数)。但是,如果用户多次玩游戏,你会在他们每次玩游戏时贴到他们的墙上吗?如果没有,您将需要创建一个“状态”作为某个点。使用这种方法,最好创建状态服务器端,并跟踪您已经发布的墙,不要再发布。
<!DOCTYPE html>
<html>
<body>
<div id="fb-root"></div>
<a href="#" onclick="postToWall();return false;">Post To Wall</a>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({ appId  : '**yourAppID**', status : true, cookie : true, xfbml  : true });

  function postToWall() {  
    FB.login(function(response) {
      if (response.session) {
        FB.api(
          {
            method: 'fql.query',
            query: 'SELECT post_id, message from stream where app_id = **yourAppID** and source_id = me()'
          },
          function(response) {
            if(response.length == 0){
              FB.ui(
              {
                 method: 'feed',
                 name: 'Facebook Dialogs',
                 link: 'https://developers.facebook.com/docs/reference/dialogs/',
                 picture: 'http://fbrell.com/f8.jpg',
                 caption: 'Reference Documentation',
                 description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
                 message: 'Facebook Dialogs are easy!'
              },
              function(response) {
                if (response && response.post_id) {
                   alert('Post was published.');
                } else {
                  alert('Post was not published.');
                }
              }
             );         
            }
            else { 
              alert('User already posted this message');
            }
          }
        );
      }
    } , {perms:''}); 
}
</script>
</body>
</html>