简体   繁体   中英

Meteor how to get access to Facebook Graph Api for both Server and Client side

accounts-facebook package provides only logIn and logOut functionality.

meteor-fbgraph gives access to fbgraph on server side.

facebook-sdk gives access to fbgraph on client side.

The problem is that facebook-sdk doesn't use anything provided with Accounts-ui, such as Accounts.onLogin event or Accounts.ui.config . After click on {{> loginButtons}} when user is logged in only Meteor.user() does log out, facebook-sdk still has it's AccessToken and remains logged in. In the result half of the application remains logged in (client) and half logged out (server).

Here is my workaround by pairing Accounts with FB events, but I think it's not a proper solution.

Accounts.onLogin(function(){
  FB.login();
  AccountsOnLogout(function(){
    FB.logout();
  });
});

function AccountsOnLogout(callback){
  var waitForLogout = setInterval(function() {
    if (!(Meteor.user())) {
      console.log("logged out");
      callback();
      clearInterval(waitForLogout);
    }
  }, 1000);
}

Do You have any better idea how to get to fbGraph on client side?

I am using only "kinda workaround" for you, cause I would be manipulating and caching respond data on the server anyway. So I just call methods and use that server side.

Facebook = (accessToken) ->
  @fb = Meteor.npmRequire 'fbgraph'
  @accessToken = accessToken
  @fb.setAccessToken @accessToken
  @options =
    timeout: 3000
    pool:
      maxSockets: Infinity
    headers:
      connection: "keep-alive"
  @fb.setOptions @options


FBQuery = (query, method, fbObject) ->
  if typeof method is 'undefined' then method = 'get'
  console.log "query is: " + query
  data = Meteor.sync((done) ->
    fbObject[method](query, (err, res) ->
      done(null, res)
    )
  )
  data.result


Meteor.methods(
  getUserData: ->
    fb = new Facebook(Meteor.user().services.facebook.accessToken)
    FBQuery '/me', 'get', fb

  getUserEvents: ->
    fb = new Facebook(Meteor.user().services.facebook.accessToken)
    FBQuery '/' + Meteor.user().services.facebook.id + '/events', 'get', fb

  getUserGroups: ->
    fb = new Facebook(Meteor.user().services.facebook.accessToken)
    FBQuery '/' + Meteor.user().services.facebook.id + '/groups?fields=name&limit=1000', 'get', fb
)

And client side

Template.home.events(
  'click #btn-user-data': (e) ->
    Meteor.call('getUserData', (err, data) ->
      $('#result').text(JSON.stringify(data, undefined, 4))
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM