How to use facebook publish stream method in a 

2019-09-09 06:47发布

I am using bellow code which is in a '.php' file, to publish stream.

<script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init( 
      {
        appId  : 'MY_APP_ID',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
      });

      var publish = {
        method: 'stream.publish',
        message: '',
        picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
        link : 'http://apps.facebook.com/puzzlegts/',
        name: 'Go to image',
        caption: 'taged you in a photo',
        description: 'test 2',
        actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
        };
        FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});

    </script>

Now I have a javascript file named 'publish.js' and I want to add publish stream method to a function in my 'publish.js' file. Can anyone please tell me how to do this?
Problem arise since I can't use '<script>' tag in a '.js' file.Therefore I can't include bellow code.

<script src="http://connect.facebook.net/en_US/all.js"></script>  

Or can anyone please post the suitable code for a '.js' file

2条回答
啃猪蹄的小仙女
2楼-- · 2019-09-09 07:31

Add this to your page

<script src="publish.js"></script>

and add this to your publish.js

  FB.init( 
  {
    appId  : '266000346762008',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });

  var publish = {
    method: 'stream.publish',
    message: '',
    picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
    link : 'http://apps.facebook.com/puzzlegts/',
    name: 'Go to image',
    caption: 'taged you in a photo',
    description: 'test 2',
    actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
    };
    FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});

(without script tags)

查看更多
地球回转人心会变
3楼-- · 2019-09-09 07:38

You have add it programmatically then or just use the asynchronous loading:

window.fbAsyncInit = function() {
    FB.init({
        appId  : 'MY_APP_ID',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
    });
    var publish = {
        method: 'stream.publish',
        message: '',
        picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
        link : 'http://apps.facebook.com/puzzlegts/',
        name: 'Go to image',
        caption: 'taged you in a photo',
        description: 'test 2',
        actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
    };
    FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});
};
(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);
}());

Notes:

  • You need to insure that you have the fb-root div is in your markup
  • this will only fire once the library all.js is fully loaded
查看更多
登录 后发表回答