我目前正在尝试进行一个简单的 API 调用来显示一些用户数据(包括好友列表)。
我有一个有效的 Facebook 登录名,具有以下请求权限 (
'click .facebook': function() {
Meteor.loginWithFacebook({ requestPermissions: ['email', 'read_friendlists', 'accesToken']},
function (error) {
if (error) {
return console.log(error);
}
});
},
然后我通过初始化facebok
Template.friendsList.rendered = function(){
window.fbAsyncInit = function() {
FB.init({
appId : '278237565717322',
xfbml : true,
version : 'v2.0'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
我想使用
进行简单的 API 调用"click .facebookfriends": function(){
FB.api('/me', function(response) {
console.log(response)
});
}
这给了我以下错误:
code: 2500
message: "An active access token must be used to query information about the current user."
type: "OAuthException"
我尝试了几个例子,但似乎找不到一种方法来登录 Facebook 并获取正确的访问 token 。有人可以帮助我设置正确的访问 token 吗?非常感激!
最佳答案
看起来您正在将 Meteor 包 Facebook OAuth 登录流程与 Facebook 自己的 JS SDK 混合在一起。每当我在 Meteor 上使用 FB 做东西时,我都会避免使用 JS SDK 并纯粹选择 Meteor 包。不管怎样,你的问题似乎是你通过 Meteor 包获取访问 token ,但当你调用 FB.api({ ... }) 时 FB JS SDK 无法访问它
尝试如下:
服务器:
Meteor.methods({
fb_me: function() {
var user = Meteor.users.findOne(this.userId);
//note: I don't have access to a meteor project hooked up to the FB API
//so where the access token is stored in a user object may differ,
//I got this from an old project. Try logging user here to find it
//if this doesn't work
var accessToken = user.services.facebook.accessToken;
if (!user || !accessToken)
throw new Meteor.Error(500, "Not a valid Facebook user logged in");
return HTTP.get("https://graph.facebook.com/me", {
params: {access_token: accessToken}}).data;
}
});
客户:
"click .facebookfriends": function(){
Meteor.call('fb_me', function(err, res) {
if (!err)
console.log(res);
}
}
这根本不需要使用 FB JS SDK。
由于您可能会让您的客户端进行大量 API 调用,因此我建议您创建一个 Meteor.method,该方法将 API 端点(例如/me)作为参数,并向客户端返回数据,类似于Facebook提供的FB.api方法。
关于javascript - Meteor:Facebook 基本 API 调用错误:访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25423346/