我已安装Meteor http://php.net/manual/en/functions.user-defined.php软件包和Facebook软件包,以允许用户连接其Facebook帐户。
我在OAuth中找到了一些代码。我已经实现了一个客户端处理程序来启动授权流程
'click #facebook-connect': function(e) {
var service;
service = "facebook";
Facebook.requestCredential({
requestPermissions: ["email"]}, function(token) {
var secret;
secret = OAuth._retrieveCredentialSecret(token);
return Meteor.call("userAddOAuthCredentials", token, service, secret, function(err, resp) {
if (err != null) {
return Meteor.userError.throwError(err.reason);
}
});
});
},
以及将Facebook令牌添加到数据库的服务器端方法,该方法由客户端按钮单击调用。
Meteor.methods({
'userAddOAuthCredentials': function(token, service, secret) {
var data, oldUser, ref, serviceSearch, services;
services = Meteor.user().services;
serviceSearch = {};
data = {};
switch (service) {
case "facebook":
if (services.facebook == null) {
data = (ref = Facebook.retrieveCredential(token, secret)) != null ? ref.serviceData : void 0;
services.facebook = data;
serviceSearch = {
"services.facebook.id": services.facebook.id
};
} else {
throw new Meteor.Error(500, "You already have a linked Facebook account with email " + services.facebook.email + "...");
}
}
oldUser = Meteor.users.findOne(serviceSearch);
if (oldUser != null) {
throw new Meteor.Error(500, "Your " + service + " account has already been assigned to another user.");
}
Meteor.users.update(this.userId, {
$set: {
services: services
}
});
if (data.email != null) {
if (!_.contains(Meteor.user().emails, data.email)) {
return Meteor.users.update(this.userId, {
$push: {
"emails": {
address: data.email,
verified: true
}
}
});
}
}
}
});
运行时我得到这个堆栈跟踪:
I20150627-13:23:00.794(-4)? Exception while invoking method 'userAddOAuthCredentials' TypeError: Cannot read property 'id' of undefined
I20150627-13:23:00.794(-4)? at [object Object].Meteor.methods.userAddOAuthCredentials (app/server/methods/facebook.js:13:52)
I20150627-13:23:00.794(-4)? at maybeAuditArgumentChecks (/Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/ddp.js:2445:12)
I20150627-13:23:00.794(-4)? at _.extend.protocol_handlers.method.exception (/Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/ddp.js:1476:20)
I20150627-13:23:00.795(-4)? at [object Object]._.extend.withValue (/Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/meteor.js:989:17)
I20150627-13:23:00.795(-4)? at /Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/ddp.js:1475:41
I20150627-13:23:00.795(-4)? at [object Object]._.extend.withValue (/Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/meteor.js:989:17)
I20150627-13:23:00.795(-4)? at [object Object]._.extend.protocol_handlers.method (/Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/ddp.js:1474:51)
I20150627-13:23:00.795(-4)? at _.extend.processMessage.processNext (/Users/raddevon/Documents/projects/resque/app/.meteor/local/build/programs/server/packages/ddp.js:1374:43)
客户端代码永远不会得到服务器端方法的秘密。为什么会发生这种情况,如何获得正确的客户端密钥以传递给我的服务器方法?