我从事的一个项目将Facebook页面作为其数据源之一。它周期性地从它导入一些数据,不涉及GUI。然后我们使用一个网络应用程序来显示我们已经拥有的数据。

并非所有信息都是公开的。这意味着我必须访问一次数据,然后保存它。然而,我不知道这个过程,我还没有找到一个好的教程。我想我需要一个access_token,我怎么能从用户得到它,一步一步?用户是一个facebook页面的管理员,他是否必须添加一些我们的FB应用程序的页面?

编辑:感谢@phwd的提示。我做了一个教程,如何获得一个永久的页面访问令牌,即使offline_access不再存在。

编辑:我刚刚发现它的答案在这里:持久的FB访问令牌服务器拉FB页面信息


当前回答

我根据donut的回答创建了一个小的NodeJS脚本。将以下文件存储在名为get-facebook-access-token.js的文件中:

const fetch = require('node-fetch');
const open = require('open');

const api_version = 'v9.0';
const app_id = '';
const app_secret = '';
const short_lived_token = '';
const page_name = '';

const getPermanentAccessToken = async () => {
  try {
    const long_lived_access_token = await getLongLivedAccessToken();
    const account_id = await getAccountId(long_lived_access_token);
    const permanent_page_access_token = await getPermanentPageAccessToken(
      long_lived_access_token,
      account_id
    );
    checkExpiration(permanent_page_access_token);
  } catch (reason) {
    console.error(reason);
  }
};

const getLongLivedAccessToken = async () => {
  const response = await fetch(
    `https://graph.facebook.com/${api_version}/oauth/access_token?grant_type=fb_exchange_token&client_id=${app_id}&client_secret=${app_secret}&fb_exchange_token=${short_lived_token}`
  );
  const body = await response.json();
  return body.access_token;
};

const getAccountId = async (long_lived_access_token) => {
  const response = await fetch(
    `https://graph.facebook.com/${api_version}/me?access_token=${long_lived_access_token}`
  );
  const body = await response.json();
  return body.id;
};

const getPermanentPageAccessToken = async (
  long_lived_access_token,
  account_id
) => {
  const response = await fetch(
    `https://graph.facebook.com/${api_version}/${account_id}/accounts?access_token=${long_lived_access_token}`
  );
  const body = await response.json();
  const page_item = body.data.find(item => item.name === page_name);  
  return page_item.access_token;
};

const checkExpiration = (access_token) => {
  open(`https://developers.facebook.com/tools/debug/accesstoken/?access_token=${access_token}&version=${api_version}`);
}

getPermanentAccessToken();

填入常量,然后运行:

npm install node-fetch
npm install open
node get-facebook-access-token.js

运行脚本后,浏览器中会打开一个页面,显示令牌及其有效期。

其他回答

我找到了这个答案,它指的是这个工具,它真的很有用。

我希望当你读到这篇文章时,这个答案仍然有效。

While getting the permanent access token I followed above 5 steps as Donut mentioned. However in the 5th step while generating permanent access token its returning the long lived access token(Which is valid for 2 months) not permanent access token(which never expires). what I noticed is the current version of Graph API is V2.5. If you trying to get the permanent access token with V2.5 its giving long lived access token.Try to make API call with V2.2(if you are not able to change version in the graph api explorer,hit the API call https://graph.facebook.com/v2.2/{account_id}/accounts?access_token={long_lived_access_token} in the new tab with V2.2) then you will get the permanent access token(Which never expires)

我尝试了这些步骤: https://developers.facebook.com/docs/marketing-api/access#graph-api-explorer

获取永久页面访问令牌

进入图形API资源管理器 在应用程序中选择你的应用程序 将长寿命访问令牌粘贴到access token中 在“访问令牌”旁边,选择要为其设置访问令牌的页面。访问令牌显示为一个新字符串。 单击i查看此访问令牌的属性 再次点击“打开访问令牌工具”按钮,打开“访问令牌调试器”工具,检查属性

一个提示,它只适用于我的网页语言是英语。

截至2020年4月,我之前的永久页面令牌开始在1到12小时内到期。我开始使用具有manage_pages权限的用户令牌来实现前面的目标(轮询页面的事件)。这些代币似乎是永久的。

我根据这篇文章中找到的信息创建了一个python脚本,托管在github.com/k-funk/facebook_permanent_token上,以跟踪需要哪些参数,以及获取永久令牌的方法是有效的。

除了上面提到的方法,值得一提的是,对于服务器到服务器的应用程序,你也可以使用这种形式的永久访问令牌: app_id | app_secret 这种类型的访问令牌称为App令牌。它通常可用于调用Graph API并在应用程序后端查询公共节点。 这里提到:https://developers.facebook.com/docs/facebook-login/access-tokens