0

我试图找到一个要求访问 facebook 应用程序的插件,如果您确认它,那么您访问的每个帖子都应该自动发布在用户墙上。我在几个页面上看到过这个例子:www.hajgare.com, www.flej.eu 因为我什么都找不到,所以我开始自己构建它,这就是我到目前为止所做的:检查用户是否登录 facebook 并检查他是否已批准该应用程序。如果是,则显示 facebook 分享框(www.balkanews.net 点击任何新闻)。

我的问题是如何在不显示该 facebook 共享框的情况下进行共享。

<div id="fb-root"></div>
<script type="text/javascript">
        $(function() {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol +
            '//connect.facebook.net/de_DE/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }());

            $(function(){

                liked = true;
                $("#trigger").click(function(){
                    if(liked){
                        post();
                    }else{
                        //alert('{L_FIRST_CLICK_LIKE}');
                        alert('like');
                    }
                });
            });


            window.fbAsyncInit = function() {
                FB.Event.subscribe('edge.create', function(response) {
                    <!-- IF BOTH -->
                    liked = true;
                    <!-- ELSE -->
                    release();
                    <!-- ENDIF -->
                }); 
                FB.Event.subscribe('edge.remove', function(response) {
                    window.location.reload();
                });
            };
        <!-- ENDIF -->

        function release() {
            $("#item_container").hide();
            $("#real_container").show();
        }

        function post(){

            FB.init({appId: "409010965841527", status: true, cookie: true,        xfbml: true});
            FB.ui({
                    method: 'feed',
                    link:   document.URL,
                }, callback);
            setTimeout('move()', 100);
        }

        function callback(response) {
            if(response && response.post_id){
                release();
            }
        }

        function move(){
            $(".fb_dialog").css("left", "38px");
            setTimeout('move()', 400);
        }
    </script>

 <!- ///////////// -->
 <a href="javascript:;" id="trigger"><img src="./template/images/share.png" alt="" /></a>
 <script>
 window.fbAsyncInit = function() {

FB.init({appId: '409010965841527', status: true, cookie: true,
         xfbml: true});

FB.getLoginStatus(function(o) { 
   if (!o && o.status) return;
   if (o.status == 'connected') {
    //here goes the post code
    post();

   } else if (o.status == 'not_authorized') {
      window.location = 'https://www.facebook.com/dialog/oauth/?client_id=409010965841527&redirect_uri='+document.URL+'/&scope=user_about_me,publish_actions,user_likes';

   } else {
     window.location = 'https://www.facebook.com/dialog/oauth/?client_id=409010965841527&redirect_uri='+document.URL+'/&scope=user_about_me,publish_actions,user_likes';
   }
});

};

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

1 回答 1

0

你的代码很好,但是你做错了 facebook 发布操作然后不要使用 FB.ui 使用 Fb.Api 来解决你的问题示例代码

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

    FB.api('/me/feed', 'post', { body: body ,message: 'I visited this post ' , link:'<?php echo wp_get_shortlink(get_the_ID()); ?>'}, function(response) {
      if (!response || response.error) {
        window.location = 'https://www.facebook.com/dialog/oauth/?client_id=409010965841527&redirect_uri='+document.URL+'/&scope=user_about_me,publish_actions,user_likes'; 
      } else {
        alert('Post published '); // you can del this alert 
      }
    });
}

就是这样,
让我知道此代码是否适合您并发布您的最终代码

于 2013-01-20T18:37:51.987 回答