我正在开发一个将facebook页面作为其数据源之一的项目。它定期从中导入一些数据,不涉及GUI。然后我们使用Web应用程序显示我们已有的数据。
并非所有信息都是公开的。这意味着我必须访问一次数据然后保留它。但是,我不知道这个过程,我还没有找到一个很好的教程。我想我需要一个access_token
,我如何从用户那里逐步获得它?用户是Facebook页面的管理员,他是否必须在页面中添加一些我们的FB应用程序?
编辑:谢谢@phwd的提示。我做了一个教程,如何获得永久页面访问令牌,即使offline_access
不再存在。
编辑:我刚刚发现它在这里回答:Long-lasting FB access-token for server to pull FB page info
按照Facebook的extending page tokens documentation中的说明,我能够获得一个不会过期的页面访问令牌。
除非另有说明,否则我建议将Graph API Explorer用于所有这些步骤。
如果您已有应用,请跳至步骤1。
您无需更改其权限或任何内容。您只需要一个在完成访问令牌之前不会消失的应用程序。
“访问令牌”字段中显示的令牌是您的短期访问令牌。
关注来自Facebook文档的these instructions,向GET请求
https://graph.facebook.com/v2.10/oauth/access_token?grant_type=fb_exchange_token&client_id= {APP_ID}&client_secret = {app_secret}&fb_exchange_token = {short_lived_token}
输入您的应用程序的ID和密码以及上一步中生成的短期令牌。
您无法使用Graph API Explorer。由于某种原因,它会受到这个请求的困扰。我认为这是因为响应不是JSON,而是查询字符串。由于这是一个GET请求,您只需转到浏览器中的URL即可。
响应应如下所示:
{ “的access_token”: “ABC123”, “token_type”: “承载”, “expires_in”:5183791}
“ABC123”将是您的长期访问令牌。你可以把它放入Access Token Debugger进行验证。在“过期”下,它应该具有类似“2个月”的东西。
使用长期访问令牌,发出GET请求
https://graph.facebook.com/v2.10/me?access_token= {} long_lived_access_token
id
字段是您的帐户ID。你需要它用于下一步。
发出GET请求
https://graph.facebook.com/v2.10/ {ACCOUNT_ID} /帐户?的access_token = {long_lived_access_token}
JSON响应应该有一个data
字段,在该字段下是用户有权访问的项目数组。找到您想要永久访问令牌的页面的项目。 access_token
字段应该有您的永久访问令牌。复制它并在Access Token Debugger中测试它。在“过期”下,它应该说“从不”。
达到应用程序请求限制(#4) - FB API v2.1及更高版本
这个答案让我得到了“我们的最终答案”,所以它非常相关所以我在这里附上它。虽然它与上述有关但它有所不同,似乎FB已经简化了一些过程。
当FB将API转到2.1版时,我们的站点上的共享计数停止工作。在我们的例子中,我们已经有了一个FB APP,我们没有使用FB登录。所以我们需要做的是获得一个FB APP令牌来发出新的请求。这是截至2016年8月23日。
/oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials
您需要从应用页面获取应用ID和应用内容。 Main FB Apps developer page{
"access_token": "app-id|app-token",
"token_type": "bearer"
}
哪里
“APP-ID”
和
“应用程序令牌”
将是您的FB应用程序页面中的应用程序ID以及刚刚收到的生成的FB App HASH。在我们的例子中,我们正在使用FB js sdk,因此我们将调用改为如此(请注意,这只会获得共享计数,而不是像过去那样合并的共享和注释计数):
FB.api(
'/','GET',{
// this is our FB app token for our FB app
access_token: FBAppToken,
"id":"{$shareUrl}","fields":"id,og_object{ engagement }"
}
这现在正常工作。这需要大量的搜索和FB的官方错误报告,以确认我们必须开始向FB api提出标记化请求。顺便说一下,我确实要求他们(FB)为提及标记化请求的错误代码(#4)添加线索。
我刚从我们的一个开发者那里得到另一份报告,由于对标记化请求的新需求,你的FB评论计数也被打破,所以我会相应地更新它。
其中许多例子不起作用,不确定是否因为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;
}
感谢@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))
我希望有人可以修剪这个代码,因为它有点混乱,但这是我能想到的唯一方法。
如果您有Facebook的应用程序,那么您可以尝试使用app-id和app-secret。
喜欢 :
access_token={your-app_id}|{your-app_secret}
它不需要经常更改令牌。
我发现this answer指的是this tool,它确实帮了很多忙。
我希望你读这篇文章时这个答案仍然有效。
这是我使用Graph API Explorer和Access Token Debugger的解决方案:
(经API API 2.9-2.11,3.0-3.1测试)
除了Vlasec答案中的建议步骤,您还可以使用:
/{pageId}?fields=access_token&access_token=THE_ACCESS_TOKEN_PROVIDED_BY_GRAPH_EXPLORER
我制作了一个PHP脚本,使其更容易。 Create an app。在Graph API Explorer中选择您的应用程序并获取具有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;
}
另一个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开始,在调试短访问令牌之后,只需单击Access Token Debugger tool底部的扩展访问令牌,就可以省去获取长访问令牌的麻烦。有关pageid
和longlivedtoken
的信息,运行下面的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。如果您错误地选择了用户令牌,第二个代码将不会结束。
我尝试了这些步骤:https://developers.facebook.com/docs/marketing-api/access#graph-api-explorer
获取永久页面访问令牌
一个提示,当页面语言是英语时,它只适用于我。
如果您只请求页面数据,则可以使用页面访问令牌。您只需要授权用户一次获取用户访问令牌;将其延长至两个月的有效期,然后请求页面的令牌。这一切都在Scenario 5中解释过。请注意,只要用户访问令牌有效,所获取的页面访问令牌才有效。
在获得永久访问令牌的同时,我按照Donut提到的5个步骤进行了操作。然而,在生成永久访问令牌的第5步中,它返回长期访问令牌(有效期为2个月)而不是永久访问令牌(永不过期)。我注意到的是当前版本的Graph API是V2.5。如果您尝试使用V2.5获取永久访问令牌,则会提供长期访问令牌。尝试使用V2.2进行API调用(如果您无法在图形api资源管理器中更改版本,请点击API调用https://graph.facebook.com/v2.2/ {在带有V2.2的新标签中,account_id} / accounts?access_token = {long_lived_access_token},然后您将获得永久访问令牌(永不过期)
除了提到的方法之外,值得一提的是,对于服务器到服务器应用程序,您还可以使用这种形式的永久访问令牌:app_id | app_secret这种类型的访问令牌称为App Token。它通常可用于调用Graph API并查询应用程序后端中的公共节点。这里提到:https://developers.facebook.com/docs/facebook-login/access-tokens