facebookfacebook-graph-api

Simple facebook API query


I am trying to do the simplest thing in the world: I want a node script to fetch the PUBLIC feed of a page.

I am trying with this:

var FB = require('fb' )

FB.api('/flourandfire/feed', 'get', function (res) {
  if (!res || res.error) {
    console.log(!res ? 'error occurred' : res.error)
    return
  }
  console.log(res.id)
  console.log(res.name)
})

And it's not working.

{ message: 'An access token is required to request this resource.',
  type: 'OAuthException',
  code: 104,
  fbtrace_id: 'EiH31CMsyvh' }

So, I created an "app" by the same user who owns the page and tried:

var FB = require('fb', { appId: 'XXXXXXXXXXXXXXXXXXXXX', appSecret: 'YYYYYYYYYYYYYYYYYYYYY' })

FB.api('/flourandfire/feed', 'get', function (res) {
  if (!res || res.error) {
    console.log(!res ? 'error occurred' : res.error)
    return
  }
  console.log(res.id)
  console.log(res.name)That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and mThat is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?ake the call work?That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?
})

Still nothing.

{ message: 'An access token is required to request this resource.',
  type: 'OAuthException',
  code: 104,
  fbtrace_id: 'EiH31CMsyvh' }

I tried adding the access token for that app:

var FB = require('fb', { appId: 'XXXXXXXXXXXXXXXXXXXXX', appSecret: 'YYYYYYYYYYYYYYYYYYYYY' })

FB.api('/flourandfire/feed', 'get', { access_token: 'ZZZZZZZZZZZZZZZZZZZZ' }, function (res) {
  if (!res || res.error) {
    console.log(!res ? 'error occurred' : res.error)
    return
  }
  console.log(res.id)
  console.log(res.name)
})

Still nothing:

{ message: 'Invalid OAuth access token.',
  type: 'OAuthException',
  code: 190,
  fbtrace_id: 'Hf8Gjudpwty' }

The feed is public. So, how the HECK do I get this feed?

That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?


Solution

  • I´ve never used the fb package, but the docs tell me that this should work:

    var FB = require('fb');
    FB.options({appId: 'xxx', appSecret: 'xxx'});
    FB.setAccessToken('APP-ID|APP-SECRET'); //btw, this is an "App Access Token". Maybe you do not even need this line if you specify the options correctly.
    //FB.options({accessToken: 'APP-ID|APP-SECRET'}); //another way according to the docs
    
    FB.api('/flourandfire/feed', (res) => {
      if (!res || res.error) {
        console.log(!res ? 'error occurred' : res.error);
        return;
      }
      console.log(res);
    });
    

    Docs: https://www.npmjs.com/package/fb

    More information about Tokens: