且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Meteor:Facebook基本API调用错误:访问令牌

更新时间:2022-05-03 07:06:20

看起来您正在将Facebook OAuth登录流程与Facebook自己的JS SDK混合在一起。每当我在Meteor上使用FB时,我就避免使用JS SDK,而是选择纯粹的Meteor包。无论你的问题是你通过meteor包获得访问令牌,但是当你调用FB.api({...})时,FB JS SDK无法访问它。

It looks like you are mixing the Meteor package Facebook OAuth login flow with Facebook's own JS SDK. Whenever I did stuff with FB on Meteor I avoided the JS SDK and opted purely for the Meteor package. Regardless it seems your problem is that you are getting the access token via the meteor package but the the FB JS SDK has no access to it when you call FB.api({ ... })

尝试类似:

服务器:

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。

That works without having to use the FB JS SDK at all.

由于你可能会让你的客户端进行大量的API调用,我建议你制作一个带有API的Meteor.method端点(例如/ me)作为参数并将数据返回给客户端,类似于Facebook提供的FB.api方法。

Since it's likely you would be letting your client make a lot of API calls I would suggest you make a single Meteor.method that takes a API endpoint (e.g. /me) as an argument and returns data to the client similar to the FB.api method Facebook provide.