Can Facebook's Javascript SDK work together wi

2019-08-02 06:14发布

Can the current Facebook Javascript SDK work with older Facebook API library?

Right now there is code to load the current Facebook Javascript SDK by:

window.fbAsyncInit = function() {
    FB.init({appId: '218565466928', status: true, cookie: true,
             xfbml: true});
};
// [...] initialize it

And there is code to use the old Facebook API by

init_fb_connect('Connect', 'XFBML', :js => :jquery, :app_settings=> '{ifUserNotConnected: fb_user_not_connected}' ) do

which is the Facebooker Rubygem. Can they work together somehow? If I have both, then the newly added "Like" button won't work. If I remove the older Facebooker code, then the "Login with Facebook" and "Share" button won't work. Any ideas?


Update: the older code do things like:

<a class="facebook-connect-link " href="#" 
  onclick="; FB.Connect.requireSession(fb_after_connect, null, true); return false;"
  rel="nofollow">Sign in with Facebook</a>  

and

<a href="#" onclick="FB.Connect.streamPublish('', {'name': 'Some product name' ...

and

  $('.login-button').each(function() {
    FB.XFBML.Host.addElement(new FB.XFBML.LoginButton(this));  
  })

1条回答
Viruses.
2楼-- · 2019-08-02 06:50

Converting the JavaScript API is relatively easy. I am not sure how much your server side will be affected though. Here are the basic methods that you would probably need:

//Check if user is logged in right now.
FB.getLoginStatus(function(response) {
  if (response.session) {
    // logged in and connected user, someone you know
  } else {
    // no user session available, someone you dont know
  }
});

//Callback fired when user logs out/logs in.
FB.Event.subscribe('auth.sessionChange', function(response) {
  // do something with response.session
});

//To force login (on login btn click).
FB.login(function(response) {
  if (response.session) {
    // user successfully logged in
    fb_after_connect();
  } else {
    // user cancelled login
  }
});

//Post to feed.
FB.api('/me/feed', 'post', { body: "message" }, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post ID: ' + response);
  }
});

If you don't want to convert to the new API then you can embed like button as iframe. Sooner or later you would have to convert your project anyway so might as well do it now.

查看更多
登录 后发表回答