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

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

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

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


当前回答

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)

其他回答

另一个PHP答案使生活更简单。更新为Facebook Graph API 2.9。把它装满,装上。

<?php
$args=[
/*-- Permanent access token generator for Facebook Graph API version 2.9 --*/
//Instructions: Fill Input Area below and then run this php file
/*-- INPUT AREA START --*/
    'usertoken'=>'',
    'appid'=>'',
    'appsecret'=>'',
    'pageid'=>''
/*-- INPUT AREA END --*/
];
echo 'Permanent access token is: <input type="text" value="'.generate_token($args).'"></input>';
function generate_token($args){
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/oauth/access_token?grant_type=fb_exchange_token&client_id={$args['appid']}&client_secret={$args['appsecret']}&fb_exchange_token={$args['usertoken']}")); // get long-lived token
    $longtoken=$r->access_token;
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/me?access_token={$longtoken}")); // get user id
    $userid=$r->id;
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/{$userid}?fields=access_token&access_token={$longtoken}")); // get permanent token
    if($r->id==$args['pageid']) $finaltoken=$r->access_token;
    return $finaltoken;
}
?>

附录(备选):

从图2.9开始,在调试了短访问令牌之后,只需单击访问令牌调试器工具底部的扩展访问令牌,就可以跳过获取长访问令牌的许多麻烦。掌握了pageid和longlivetoken的信息后,运行下面的php以获得永久访问令牌。

<?php
$args=[
/*-- Permanent access token generator for Facebook Graph API version 2.9 --*/
//Instructions: Fill Input Area below and then run this php file
/*-- INPUT AREA START --*/
    'longlivedtoken'=>'',
    'pageid'=>''
/*-- INPUT AREA END --*/
];
echo 'Permanent access token is: <input type="text" value="'.generate_token($args).'"></input>';
function generate_token($args){
$r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/{$args['pageid']}?fields=access_token&access_token={$args['longlivedtoken']}"));
return $r->access_token;
}
?>

尽管第二个代码为您节省了很多麻烦,但我建议运行第一个php代码,除非您非常着急,因为它会交叉检查pageid和userid。如果您错误地选择了user令牌,则第二个代码将无法工作。

感谢dw1和Rob

我做了一个PHP脚本,使它更容易。创建一个应用程序。在Graph API资源管理器中选择你的应用程序,并获得具有manage_pages和publish_pages权限的用户令牌。在关于页面的底部找到页面的ID。填写配置变量并运行脚本。

<?php
$args=[
    'usertoken'=>'',
    'appid'=>'',
    'appsecret'=>'',
    'pageid'=>''
];

echo generate_token($args);

function generate_token($args){
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.8/oauth/access_token?grant_type=fb_exchange_token&client_id={$args['appid']}&client_secret={$args['appsecret']}&fb_exchange_token={$args['usertoken']}")); // get long-lived token
    $longtoken=$r->access_token;
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.8/me?access_token={$longtoken}")); // get user id
    $userid=$r->id;
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.8/{$userid}/accounts?access_token={$longtoken}")); // get permanent token
    foreach($r->data as $d) if($d->id==$args['pageid']) return $d->access_token;
}

我尝试了这些步骤: 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上,以跟踪需要哪些参数,以及获取永久令牌的方法是有效的。

上面的大多数答案现在都没有给出永久代币,他们只延长到2个月。我是这样得到它的:

从图形资源管理器工具中,选择相关权限并获得短时间页面访问令牌。 转到调试器工具并粘贴您的访问令牌。然后,单击页面底部的“扩展令牌”按钮。 复制扩展的令牌并在这个API中使用它: https://graph.facebook.com/v2.10/me?fields=access_token&access_token= < extended_access_token > 这将返回永久访问令牌。你可以在调试器工具中验证,expires at字段应该说“Never”。