简单的facebook API查询

时间:2018-01-04 09:09:45

标签: facebook facebook-graph-api graph

我正在尝试做世界上最简单的事情:我想要一个节点脚本来获取页面的PUBLIC提要。

我正在尝试这个:

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

它不起作用。

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

所以,我创建了一个拥有该页面的同一个用户的“app”并试过:

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

仍然没有。

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

我尝试为该应用添加访问令牌:

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

仍然没有:

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

Feed是公开的。 那么,我如何得到这个饲料?

也就是说,EASIEST,最永久的方式是获得正确的访问令牌/ appId / appSecret /什么来探究这些功能并使呼叫有效?

1 个答案:

答案 0 :(得分:0)

我从未使用过fb软件包,但文档告诉我这应该可行:

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

文档:https://www.npmjs.com/package/fb

有关令牌的更多信息: