开发者

How to post on facebook wall, without opening dialog box?

开发者 https://www.devze.com 2023-04-09 23:33 出处:网络
I\'am having a quiz program on my site where user logs in through their facebook account and play the quiz.

I'am having a quiz program on my site where user logs in through their facebook account and play the quiz.

After successfully completing the quiz I want to post their score and rank on their wall. Facebook login and logout is working fine. How do I show their score and rank on their wall without displaying a dialog box?

I'm using thi开发者_高级运维s code to post the score and rank on user's wall-

require 'src/facebook.php';

<?php
    $facebook = new Facebook(array(
        'appId'  => 'APP ID',
        'secret' => 'SECRET KEY',
    ));
?>
<script>
    FB.init({ 
        appId:'APP ID', cookie:true, 
        status:true, xfbml:true 
    });

    FB.ui({ method: 'feed', 
        message: '<?php echo "My score is ".$myscore." and rank is ".$rank; ?>'});
</script>

This code is opening a dialog box for me but that is with empty input field. If user will hust click on 'Publish' button then nothing will be posted on their wall and if they edit the text in input field then they will post wrong information. Either this field should be un editable or this box should not be there.

Please suggest me a solution with code example.


That's because FB not allowing pre-editing the message field. If you want to publish something in the user's Wall, you should have 'publish_stream' permission from the user, that way you will be able to post messages without requesting the user to enter something in it's message box or pressing the Publish button. But for this you have to call FB.api to access Graph API to access the user's Wall. For more details, see Graph API.

Adding to this code you wrote, you should use OAuth2 methods by adding 'oauth: true' to the FB.init parameter.


You should request "publish_stream" permissions to be able to publish Feed Stories without showing dialog to user.

Sample code may look like this:

FB.login(function(response) {
  if (response.authResponse) {
    console.log('Posting message');
    FB.api('/me', 'post', {message: 'Beware, boiling water...'});
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'publish_stream'});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号