开发者

Automated facebook post to wall with Javascript

开发者 https://www.devze.com 2023-03-28 20:28 出处:网络
I have a facebook application in which the user is authenticated with PHP and grants permissions to the app, including publish_stream.

I have a facebook application in which the user is authenticated with PHP and grants permissions to the app, including publish_stream.

During the application, the user is going through several screens.

On the last screen, the is user chooses if they want to share a post on their wall.

If they do, an automated and formatted post should be posted on their wall.

I've tried to do it with Javascript but it didn't work. Can you see what's wrong?

Thanks! Here's my code:

<div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId  : 'MY APP ID',
          status : true, 
          cookie : true, 
          xfbml  : true 
        });
      };

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

<script>
    function postToFacebook() {
        var body = '';
        var params = {};
        params['message'] = 'MESSAGE';
        params['name'] = 'NAME';
        params['description'] = '';
        params['link'] = '';
        params['picture'] = 'https://www.URL.com/pic.jpg';
        params['caption'] = 'CAPTION';
        FB.api('/me/feed', 'post', params, function(response) {
          if (!response || response.error) {
           // alert('Error occured');
          } else {
           // alert('Post ID: ' + response);
          }
        });
    }
    </scr开发者_开发百科ipt>


From reading your question and the various comments it seems to me that the users session information is not persisting into the JavaScript SDK - this assumes that there is a valid user session being maintained serverside.

First of all you should check that you are using the most up to date PHP SDK. To double check download and install the latest version from GitHub. I think this should solve your problem as the cookies containing the authorised users session data should be passed between the PHP and JavaScript SDKs.

If that doesn't work I have a suspicion that the user is not being authenticated correctly serverside. In which case you could try the following.

Before you postToFacebook() you should check the users the users logged in status and log them in if necessary. For example:

FB.getLoginStatus(function(response) {
    if (response.authResponse) {
        // logged in and connected user, someone you know
        postToFacebook();
    } else {
        // no user session available, someone you dont know
        FB.login(function(response) {
            if (response.authResponse) {
                // logged in and connected user
                postToFacebook();
            } else {
                // User cancelled login or did not fully authorize
            }
        }, {scope: 'YOUR,REQUIRED,PERMISSIONS'});
    }
});


You are not calling the function postToFacebook()!

window.fbAsyncInit = function() {
    FB.init({
      appId  : 'MY APP ID',
      status : true, 
      cookie : true, 
      xfbml  : true 
    });
    postToFacebook();
  };


When you attempt to do this:

FB.api('/me/feed', 'post', params, function(response) { ..  });

You need to pass the access token along with the call. I assume you have it on the php/server side, so then:

FB.api('/me/feed/access_token='[INSERT_ACCESS_TOKEN], 'post', params, function(response) { ..  });
0

精彩评论

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