【问题标题】:Simple facebook API query简单的 facebook API 查询
【发布时间】:2018-01-04 09:09:44
【问题描述】:

我正在尝试做世界上最简单的事情:我想要一个节点脚本来获取页面的 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' }

因此,我由拥有该页面的同一用户创建了一个“应用程序”并尝试:

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 是公开的。 那么,我到底是如何获得这个提要的呢?

也就是说,获取正确的访问令牌/appId/appSecret/whatever 以进入这些函数并使调用正常工作的最简单、最永久的方法是什么?

【问题讨论】:

  • 然后提供一个 valid 令牌,而不是无效的...?如果页面是公开的并且帖子不是针对特定受众的,那么应用访问令牌就可以了。并且,最简单的形式是,如果应用程序 ID 和应用程序机密的组合以及中间的管道符号。
  • 这实际上修复了它。你能把它设置为答案,以便我接受吗?
  • 这实际上是我的回答...它告诉您应用访问令牌的外观。您还可以在我的回答中的链接中阅读有关该内容(以及其他形式的令牌)的信息。

标签: facebook facebook-graph-api graph


【解决方案1】:

我从未使用过 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

更多关于代币的信息:

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多