facebook:永久页面访问令牌?
我在一个项目中工作,该项目将facebook页面作为其数据源之一。它定期从中导入一些数据,而不涉及GUI。然后我们使用一个web应用程序来显示我们已经拥有的数据 并不是所有的信息都是公开的。这意味着我必须访问数据一次,然后保存它。然而,我不知道这个过程,我还没有找到一个很好的教程。我想我需要一个facebook:永久页面访问令牌?,facebook,facebook-graph-api,facebook-access-token,Facebook,Facebook Graph Api,Facebook Access Token,我在一个项目中工作,该项目将facebook页面作为其数据源之一。它定期从中导入一些数据,而不涉及GUI。然后我们使用一个web应用程序来显示我们已经拥有的数据 并不是所有的信息都是公开的。这意味着我必须访问数据一次,然后保存它。然而,我不知道这个过程,我还没有找到一个很好的教程。我想我需要一个访问\u令牌,如何从用户那里一步一步地获得它?该用户是facebook页面的管理员,他是否需要向该页面添加我们的FB应用程序 编辑:谢谢@phwd的提示。我做了一个如何获得永久页面访问令牌的教程,即使of
访问\u令牌
,如何从用户那里一步一步地获得它?该用户是facebook页面的管理员,他是否需要向该页面添加我们的FB应用程序
编辑:谢谢@phwd的提示。我做了一个如何获得永久页面访问令牌的教程,即使offline\u-access
不再存在
编辑:我刚刚发现这里的答案是:如果您只请求页面数据,那么您可以使用页面访问令牌。您只需授权用户一次即可获得用户访问令牌;将其有效期延长到两个月,然后为页面请求令牌。这些都在本文中进行了解释。请注意,获取的页面访问令牌仅在用户访问令牌有效的情况下有效。除了答案中建议的步骤外,您还可以使用:
- 要进行查询,例如,
/{pageId}?fields=access\u-token&access\u-token=由\u-GRAPH\u-EXPLORER提供的\u-access\u-token\u
- 获取有关访问令牌的信息
<?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;
}
?>
1.获取用户短期访问令牌
<?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;
}
?>
2.生成长寿命访问令牌
根据Facebook文档,向
{app\u id}&客户端\u秘密={app\u秘密}&fb\u交换\u令牌={short\u live\u令牌}
输入应用程序的ID和密码以及上一步生成的短期令牌
您不能使用图形API资源管理器。出于某种原因,它在这个请求上被卡住了。我认为这是因为响应不是JSON,而是一个查询字符串。由于这是一个GET请求,您只需在浏览器中转到URL即可
响应应如下所示:
{“访问令牌”:“ABC123”,“令牌类型”:“承载者”,“到期日”:5183791}
“ABC123”将是您的长期访问令牌。您可以将其放入以进行验证。在“Expires”下,应该有“2个月”之类的内容
3.获取用户ID
使用长寿命访问令牌,向
{long\u live\u access\u token}
id
字段是您的帐户id。下一步需要它
4.获取永久页面访问令牌
向…发出GET请求
{account\u id}/accounts?access\u token={long\u live\u access\u token}
JSON响应应该有一个
data
字段,该字段下是用户可以访问的项目数组。查找要从中获取永久访问令牌的页面的项目。access\u token
字段应该有您的永久访问令牌。复制它并在中进行测试。在“Expires”(过期)下应显示“Never”(永不)。除了上述方法之外,值得一提的是,对于服务器到服务器应用程序,您还可以使用这种形式的永久访问令牌:
应用程序id |应用程序机密
这种类型的访问令牌称为应用令牌。它通常可用于调用Graph API和查询应用程序后端中的公共节点。
这里提到:在获取永久访问令牌时,我遵循了上面提到的5个步骤。然而,在第5步中,当生成永久访问令牌时,其返回的是长寿命访问令牌(有效期为2个月)而不是永久访问令牌(永不过期)。我注意到Graph API的当前版本是V2.5。如果您试图使用V2.5获取永久访问令牌,请尝试使用V2.2进行API调用(如果您无法在graph API explorer中更改版本,请使用V2.2在新选项卡中点击API调用{account\u id}/accounts?access\u token={long\u live\u access\u token})然后您将获得永久访问令牌(永不过期)已达到(#4)的应用程序请求限制-FB API v2.1及更高版本 这个答案让我找到了“我们的最终答案”,所以它非常相关,所以我在这里附加它。虽然它与上述内容相关,但它是不同的,似乎FB简化了一些过程 当FB将api升级到V2.1时,我们网站上的共享计数停止工作。在我们的例子中,我们已经有了一个FB应用程序,我们没有使用FB登录。因此,我们需要做的是获得一个FB-APP令牌来发出新的请求。截至2016年8月23日
/oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials
你将想要去抓取你的应用程序i
"app-token"
"app-token"
FB.api(
'/','GET',{
// this is our FB app token for our FB app
access_token: FBAppToken,
"id":"{$shareUrl}","fields":"id,og_object{ engagement }"
}
<?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
$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;
}
?>
<?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;
}
?>
$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;
}
// 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))
access_token={your-app_id}|{your-app_secret}
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