Facebook JS SDK — Post to wall after permissions check

facebookfacebook-graph-apifacebook-javascript-sdk

I've been Googling for 2 days, and can't find anything definitive. All results I've come across use deprecated code that no longer works. Please help.

I need a button that on click…

  1. Checks for extended permissions
  2. If permissions are not already granted, request them (user_likes, offline_access, publish_stream)
  3. After getting permissions, publish post to user's wall
  4. Allow me to push wall posts to users while they are offline

Can someone please assist?

Best Answer

It's pretty simple. You can call FB.login to get extended permissions. Then you can call FB.ui to post a status (or FB.api to call /me/feed to post without user interaction, which is frowned upon). To be able to push wall posts at a later date you would need to store the access_token on your server for later use.

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

  function postToWall() {  
    FB.login(function(response) {
      if (response.authResponse) {
        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.'
        },
        function(response) {
          if (response && response.post_id) {
            alert('Post was published.');
          } else {
            alert('Post was not published.');
          }
        });
      } else {
        alert('User cancelled login or did not fully authorize.');
      }
    }, {scope: 'user_likes,offline_access,publish_stream'});
    return false;
}
</script>
</body>
</html>
Related Topic