2

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

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);
            }
        });
},

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

And I want to make a simple API call with

"click .facebookfriends": function(){
    FB.api('/me', function(response) {
        console.log(response)
    });
}

This gives me the following error:

code: 2500
message: "An active access token must be used to query information about the current user."
type: "OAuthException"

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!

Jaspermid
  • 461
  • 3
  • 14

1 Answers1

5

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({ ... })

Try something like:

server:

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

client:

"click .facebookfriends": function(){
    Meteor.call('fb_me', function(err, res) {
      if (!err)
        console.log(res);
    }
}

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

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.

shambles
  • 666
  • 4
  • 5
  • Thanks for the explanation. The above code gives me error: 500 errorType: "Meteor.Error" message: "Internal server error [500]" reason: "Internal server error" I'll try to find the acces token – Jaspermid Aug 21 '14 at 12:54
  • I've tried adding {} to the if statement, and added an else to see what the error is. This gives me the above error – Jaspermid Aug 21 '14 at 12:55
  • Meteor.user does not have a access token (that's probably the problem). It just has an _id and username and some proto_objects – Jaspermid Aug 21 '14 at 12:57
  • I found the accesstoken for the logged in user. The 500 error seems to be caused by HTTP.get("https://graph.facebook.com/me", { params: {access_token: accessToken}}).data; } – Jaspermid Aug 21 '14 at 13:24
  • Do you see any error output from your terminal where you launched meteor? I would expect the HTTP.get line to work since I got it from the Facebook package where meteor itself gets the users identity so it can store your real name etc. Where was the access token in the Meteor user object? You wouldn't be able to see it client side since it's not given to the client by default for security reasons – shambles Aug 21 '14 at 13:55
  • I checked both the access token and the userid. Both return valid results (through the meteor call so server side). No further information in the error unfortunately. I managed to get a valid result but through entirely different means, Following http://www.andrehonsberg.com/article/facebook-graph-api-meteor-js this tutorial. – Jaspermid Aug 21 '14 at 17:44
  • You have probably solved this but just wanted to mention. Your error is most likely related to HTTP package not being added. Just do a: `meteor add http` – Ayrton Senna Mar 13 '15 at 07:34