5

使用Meteor时,使用新的身份验证系统,使用包登录时如何获取 facebook访问令牌accounts-facebook

4

2 回答 2

17

从 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;
于 2012-09-29T22:15:24.577 回答
6

更新和简化劳埃德的答案,我们得到:

服务器

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

于 2012-10-30T16:19:48.880 回答