Facebook应用,允许访问后发布状态

时间:2014-04-10 14:29:14

标签: facebook facebook-login

好的,所以我在我的网站上通过Facebook设置了一个登录,点击它之后,它将通过Facebook验证并提出:

http://prntscr.com/38pfth

一旦他们点击播放,它将成功通过Facebook登录并将其重定向到主页。

但是在他们点击“立即播放”之后,我希望它能够通过身份验证的用户发布状态,例如“我刚刚通过Facebook登录.....”

我该怎么做? 感谢

1 个答案:

答案 0 :(得分:1)

如果用户连接/登录到您的应用/游戏时您已经要求正确的权限,那么应该非常简单。下面是我为我的一个项目编写的一些代码。用户必须明确允许应用程序代表他们发布到流。

特别注意:

{scope: 'publish_stream'}

这将在连接阶段询问用户您的游戏/应用是否可以代表用户发布到流中。

// Click Handler for the login. 
$(document).delegate('#fb-connect', 'click', function(e){
    e.preventDefault();
    FB.login(function(response) {

    // Check the status
    // console.log(response.status);

    if (response.status === 'connected') {

       // Run the function we created below to publish to the users stream.  
       publish('I just logged in via Facebook. '); // Your Message 

    } else if (response.status === 'unknown' || response.status === 'not_authorized') {
        // Something else if they chose not to connect
    } else {

    }

    }, {scope: 'publish_stream'});

});

这是一个简单的函数,您可以将字符串传递给(您的消息)并将其发布到当前连接的用户流。

function publish(messagebody) {

var body = messagebody; // This is the passed in string aka message you want to post. 


params = {
    message: body, // Your message body
    link: 'http://www.yoursite.com' // if you want to share a link. 
}

FB.api('/me/feed', 'post', params, function(response) {
  if (!response || response.error) {
    //alert('Error occured');
    console.log(response.error);
  } else {
    // Successful post to facebook.
    // alert('Post ID: ' + response.id);

  }
});
}

希望这能帮到你!

编辑:添加完整的HTML示例页面。

<html>
<head>
    <title>Your Page</title>
    <!-- Load the Facebook SDK for your APP -->
    <script>
        window.fbAsyncInit = function() {
        // init the FB JS SDK
        FB.init({
          appId   : 'YOURAPPID', // Test App ID for localhost
          channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
          status     : true,                                 // Check Facebook Login status
          xfbml      : true                                  // Look for social plugins on the page
        });

        // Additional initialization code such as adding Event Listeners goes here
      };

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


<!-- Markup for Connect Button -->
<a id="fb-connect">
    <img src="URLTOANIMAGEYOULIKEFORTHEBUTTON">
</a>

<!--  Reference to Jquery -->
<script src="PATHTOJQUERY"></script>

<!-- Some Additional Script to handle the functions / Could also be another JS file you include.  -->
<script>
$(document).ready(function(){

    // Bind the Click of the Button fb-connect
    $(document).delegate('#fb-connect', 'click', function(e){
        e.preventDefault();
        FB.login(function(response) {

        // Check the status
        // console.log(response.status);

        if (response.status === 'connected') {

           // Run the function we created below to publish to the users stream.  
           publish('I just logged in via Facebook. '); // Your Message 

        } else if (response.status === 'unknown' || response.status === 'not_authorized') {
            // Something else if they chose not to connect
        } else {

        }

        }, {scope: 'publish_stream'});

    });
});


function publish(messagebody) {

    var body = messagebody; // This is the passed in string aka message you want to post. 


    params = {
        message: body, // Your message body
        link: 'http://www.yoursite.com' // if you want to share a link. 
    }

    FB.api('/me/feed', 'post', params, function(response) {
      if (!response || response.error) {
        //alert('Error occured');
        console.log(response.error);
      } else {
        // Successful post to facebook.
        // alert('Post ID: ' + response.id);

      }
    });
}

</script>

</body>
</html>