流星访问令牌

时间:2012-09-24 18:33:24

标签: oauth meteor

使用Meteor时,使用新的authentication系统时,如果使用accounts-facebook软件包进行记录,如何获取facebook 访问令牌

2 个答案:

答案 0 :(得分:16)

从Meteor.method获取它:

// server
Meteor.methods({
  getAccessToken : function() {
    try {
      return Meteor.user().services.facebook.accessToken;
    } catch(e) {
      return null;
    }
  }
}); 

// client
Meteor.call("getAccessToken", function(error, accessToken){
   console.log(accessToken);
})

或发布:

//server
Meteor.publish("currentUserAccessToken", function(){
    var self = this;
    if (this.userId()){
        handle = Meteor.users.find(this.userId()).observe({
            added: function(user){
                self.set("currentUserAccessToken", user._id, {value: user.services.facebook.accessToken});
                self.flush();
            },
            changed: function(user){
                self.set("currentUserAccessToken", user._id, {value: user.services.facebook.accessToken});
                self.flush();
            }
        });

        this.onStop(function() {
            handle.stop();
        });
    }
});

//client
var AccessToken = new Meteor.Collection("currentUserAccessToken");
Meteor.subscribe("currentUserAccessToken");

//access the value
var accessToken = AccessToken.findOne().value;

答案 1 :(得分:6)

更新和简化Lloyd's answer,我们得到了这个:

服务器

Meteor.publish("currentAccessToken", function(){
  return Meteor.users.find(this.userId, {fields: {'services.facebook.accessToken': 1}});
});

的客户端

Meteor.autosubscribe(function(){
  var newUser = Meteor.user();
  Meteor.subscribe('currentAccessToken');
});

每次用户更改状态时都会更新,并且Meteor.user().services.facebook.accessToken可以访问访问令牌(如果存在)