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

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

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

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


当前回答

除了在Vlasec回答中推荐的步骤,您还可以使用:

图形API资源管理器进行查询,例如/{pageId}?= access_token&access_token = THE_ACCESS_TOKEN_PROVIDED_BY_GRAPH_EXPLORER字段 访问令牌调试器以获取有关访问令牌的信息。

其他回答

除了在Vlasec回答中推荐的步骤,您还可以使用:

图形API资源管理器进行查询,例如/{pageId}?= access_token&access_token = THE_ACCESS_TOKEN_PROVIDED_BY_GRAPH_EXPLORER字段 访问令牌调试器以获取有关访问令牌的信息。

以下是我的解决方案,只使用图形API资源管理器和访问令牌调试器:

Graph API Explorer: Select your App from the top right dropdown menu Select "Get User Access Token" from dropdown (right of access token field) and select needed permissions Copy user access token Access Token Debugger: Paste copied token and press "Debug" Press "Extend Access Token" and copy the generated long-lived user access token Graph API Explorer: Paste copied token into the "Access Token" field Make a GET request with "PAGE_ID?fields=access_token" Find the permanent page access token in the response (node "access_token") (Optional) Access Token Debugger: Paste the permanent token and press "Debug" "Expires" should be "Never"

(API版本2.9-2.11,3.0-3.1测试)

我根据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)

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

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