本文介绍了流星如何在服务器端和客户端都访问Facebook Graph Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帐户-facebook 软件包仅提供登录和注销功能.

accounts-facebook package provides only logIn and logOut functionality.

流星-fbgraph 允许在服务器端访问fbgraph.

meteor-fbgraph gives access to fbgraph on server side.

facebook-sdk 允许在客户端访问fbgraph.

facebook-sdk gives access to fbgraph on client side.

问题在于,facebook-sdk不使用Accounts-ui随附的任何内容,例如Accounts.onLogin事件或Accounts.ui.config.单击{{> loginButtons}}后,只有用户登录时Meteor.user()确实注销,facebook-sdk仍然具有AccessToken并保持登录状态.结果,一半的应用程序保持登录状态(客户端),另一半的应用程序退出(服务器) ).

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).

这是将Accounts与FB事件配对的解决方法,但我认为这不是一个合适的解决方案.

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);
}

您是否有更好的主意,如何在客户端使用fbGraph?

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

推荐答案

我只为您使用"kinda解决方法",因为无论如何我都会在服务器上操纵和缓存响应数据.因此,我只调用方法并使用该服务器端.

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
)

和客户端

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

这篇关于流星如何在服务器端和客户端都访问Facebook Graph Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:59