Meteor:Facebook基本API调用错误:访问令牌 [英] Meteor: Facebook basic API call error: access token

查看:59
本文介绍了Meteor:Facebook基本API调用错误:访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试进行简单的API调用以显示一些用户数据(包括朋友列表)。

I am currently trying to make a simple API call to show some user data (including a friends list).

我有一个工作的Facebook登录,具有以下请求权限(

I have a working Facebook login with the following request permission (

 'click .facebook': function() {
        Meteor.loginWithFacebook({ requestPermissions: ['email', 'read_friendlists',    'accesToken']},
        function (error) {
            if (error) {
                return console.log(error);
            }
        });
},

比我初始化facebok通过

Than I initialize facebok through

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'));

我想用

"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"

我试过几个例子但似乎找不到办法做loginWithFacebook并获得正确的accessstoken。任何人都可以帮我设置正确的访问令牌吗?非常感激!

I have tried several examples but cannot seem to find a way to do loginWithFacebook and get the proper accesstoken. Could anyone help me with getting a proper access token set up? Much appreciated!

推荐答案

看起来您正在将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.

这篇关于Meteor:Facebook基本API调用错误:访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆