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

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

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

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


当前回答

多亏了@donut,我设法在JavaScript中获得了永不过期的访问令牌。

// Initialize exchange
fetch('https://graph.facebook.com/v3.2/oauth/access_token?grant_type=fb_exchange_token&client_id={client_id}&client_secret={client_secret}&fb_exchange_token={short_lived_token}')
.then((data) => {
    return data.json();
})
.then((json) => {
    // Get the user data
    fetch(`https://graph.facebook.com/v3.2/me?access_token=${json.access_token}`)
    .then((data) => {
        return data.json();
    })
    .then((userData) => {
        // Get the page token
        fetch(`https://graph.facebook.com/v3.2/${userData.id}/accounts?access_token=${json.access_token}`)
        .then((data) => {
            return data.json();
        })
        .then((pageToken) => {
            // Save the access token somewhere
            // You'll need it at later point
        })
        .catch((err) => console.error(err))
    })
    .catch((err) => console.error(err))
})
.catch((err) => {
    console.error(err);
})

然后我像这样使用保存的访问令牌

fetch('https://graph.facebook.com/v3.2/{page_id}?fields=fan_count&access_token={token_from_the_data_array}')
.then((data) => {
    return data.json();
})
.then((json) => {
    // Do stuff
})
.catch((err) => console.error(err))

我希望有人能修改这段代码,因为它有点乱,但这是我能想到的唯一方法。

其他回答

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

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

如:

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

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

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

很多例子都不起作用,不确定是不是因为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;
}

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

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