使用 Facebook 获取页面访问令牌 API 5.0 PHP
Get page access token with Facebook API 5.0 PHP
我需要在 Facebook 页面上 post 消息。具体来说,我想通过 cron
.
post
API 文档是这样说的:
Page Access Token – These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
如何在没有页面回调的情况下获取用户访问和页面访问令牌?这可能吗?
我只有 JavaScript 代码,但是一旦你有了访问令牌,你就可以获得可以由给定用户管理的页面。这将包含每个页面访问令牌:
jQuery.ajax({type: "GET",
url: "https://graph.facebook.com/v2.2/me/accounts?access_token=" + userToken,
async: false,
data: jsonRequest,
dataType: "json",
cache: false,
success: function(data)
{
返回的数据是这样的:
{
"data": [
{
"access_token": "CAACni8TcBB0B...cZBJfwZDZD",
"category": "Computers/Technology",
"name": "abc",
"id": "...",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
{
"access_token": "CAA...ZDZD",
"category": "App Page",
"name": "xyz",
"id": "....",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
}
],
access_token
是您的页面令牌。您可以轻松地将上述请求转换为PHP。
您需要的是扩展页面令牌,它永远有效。你得到一个这样的:
- 使用
manage_pages
权限进行授权(以及 publish_pages
如果您想稍后 post 作为页面),以获取用户令牌
- 扩展用户令牌
- 将
/me/accounts?fields=access_token
与扩展用户令牌一起使用,以获取所有具有扩展页面令牌的页面的列表 - 或者使用 /page-id?fields=access_token
来获取特定页面的扩展页面令牌
关于所有令牌以及如何扩展用户令牌的信息:
PHP API V5
经过 24 小时的摸索,下面的代码对我有用....希望这对你有所帮助,如果你需要这个代码工作,你应该已经完成了前两个步骤
- 应该可以登录我用的 facebook
getRedirectLoginHelper
- 在回调文件中使用接收到的用户访问令牌设置会话变量
$_SESSION['fb_access_token'] = (string) $accessToken;
$fbApp = new Facebook\FacebookApp( 'xxx', 'xxx', 'v2.7' );
$fb = new Facebook\Facebook( array(
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.7'
) );
$requestxx = new FacebookRequest(
$fbApp,
$_SESSION['fb_access_token'],//my user access token
'GET',
'/{page-id}?fields=access_token',
array( 'ADMINISTER' )
);
$responset = $fb->getClient()->sendRequest( $requestxx );
$json = json_decode( $responset->getBody() );
$page_access = $json->access_token;
//posting to page
$requesty = new FacebookRequest(
$fbApp,
$page_access ,
'POST',
'/{page-id}/feed?message=Hello fans YYYYYYYYYYYYYYY'
);
$response = $fb->getClient()->sendRequest( $requesty );
var_dump( $response );
您可以通过以下方式获取页面令牌:
$response = $fb->get('/'.$pageId.'?fields=access_token', (string)$accessToken);
$json = json_decode($response->getBody());
$page_token = $json->access_token;
$response = $fb->post('/'.$pageId.'/feed', $fbData, $page_token);
我需要在 Facebook 页面上 post 消息。具体来说,我想通过 cron
.
API 文档是这样说的:
Page Access Token – These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
如何在没有页面回调的情况下获取用户访问和页面访问令牌?这可能吗?
我只有 JavaScript 代码,但是一旦你有了访问令牌,你就可以获得可以由给定用户管理的页面。这将包含每个页面访问令牌:
jQuery.ajax({type: "GET",
url: "https://graph.facebook.com/v2.2/me/accounts?access_token=" + userToken,
async: false,
data: jsonRequest,
dataType: "json",
cache: false,
success: function(data)
{
返回的数据是这样的:
{
"data": [
{
"access_token": "CAACni8TcBB0B...cZBJfwZDZD",
"category": "Computers/Technology",
"name": "abc",
"id": "...",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
{
"access_token": "CAA...ZDZD",
"category": "App Page",
"name": "xyz",
"id": "....",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
}
],
access_token
是您的页面令牌。您可以轻松地将上述请求转换为PHP。
您需要的是扩展页面令牌,它永远有效。你得到一个这样的:
- 使用
manage_pages
权限进行授权(以及publish_pages
如果您想稍后 post 作为页面),以获取用户令牌 - 扩展用户令牌
- 将
/me/accounts?fields=access_token
与扩展用户令牌一起使用,以获取所有具有扩展页面令牌的页面的列表 - 或者使用/page-id?fields=access_token
来获取特定页面的扩展页面令牌
关于所有令牌以及如何扩展用户令牌的信息:
PHP API V5
经过 24 小时的摸索,下面的代码对我有用....希望这对你有所帮助,如果你需要这个代码工作,你应该已经完成了前两个步骤
- 应该可以登录我用的 facebook
getRedirectLoginHelper
- 在回调文件中使用接收到的用户访问令牌设置会话变量
$_SESSION['fb_access_token'] = (string) $accessToken;
$fbApp = new Facebook\FacebookApp( 'xxx', 'xxx', 'v2.7' );
$fb = new Facebook\Facebook( array(
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.7'
) );
$requestxx = new FacebookRequest(
$fbApp,
$_SESSION['fb_access_token'],//my user access token
'GET',
'/{page-id}?fields=access_token',
array( 'ADMINISTER' )
);
$responset = $fb->getClient()->sendRequest( $requestxx );
$json = json_decode( $responset->getBody() );
$page_access = $json->access_token;
//posting to page
$requesty = new FacebookRequest(
$fbApp,
$page_access ,
'POST',
'/{page-id}/feed?message=Hello fans YYYYYYYYYYYYYYY'
);
$response = $fb->getClient()->sendRequest( $requesty );
var_dump( $response );
您可以通过以下方式获取页面令牌:
$response = $fb->get('/'.$pageId.'?fields=access_token', (string)$accessToken);
$json = json_decode($response->getBody());
$page_token = $json->access_token;
$response = $fb->post('/'.$pageId.'/feed', $fbData, $page_token);