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

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

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

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


当前回答

我做了一个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;
}

其他回答

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

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

很多例子都不起作用,不确定是不是因为2.9v的电压,但我撞到了头。无论如何,我采用了@dw1版本,并在@KFunk视频的帮助下对其进行了一些修改,并以2.9的价格让它为我工作。希望这能有所帮助。

$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/{$args['pageid']}?fields=access_token&access_token={$longtoken}")); // get user id
    $finaltoken=$r->access_token;
    return $finaltoken;
}

如果只请求页数据,则可以使用页访问令牌。您只需授权用户一次即可获得用户访问令牌;将有效期延长到两个月,然后请求该页的令牌。这在场景5中都有解释。请注意,获取的页面访问令牌仅在用户访问令牌有效期间有效。

我做了一个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;
}

如果你有facebook的应用,那么你可以尝试app-id和app-secret。

如:

access_token={your-app_id}|{your-app_secret}

它不需要频繁地更改令牌。