问题描述
值为 FACEBOOK_ *
在代码库前面定义。
// SDK版本5.0
$ fb =新的Facebook\Facebook([
'app_id'=> FACEBOOK_APP_ID,
'app_secret'=> FACEBOOK_APP_SECRET,
'default_graph_version'=>'v2.4',
]);
//返回一个Facebook\FacebookResponse对象
$ response = $ fb-> post('/'。FACEBOOK_PAGE_ID。'/ feed',$ postData,FACEBOOK_ACCESS_TOKEN);
$ postId = $ response-> getGraphNode();
现在我的问题是如何将其作为实际页面发布,而不是我的帐户页面的管理员。
我看过SDK文档,我一直在圈子里,有很多v4的例子,但是不赞成我试图使用v5,只是似乎无法弄清楚,任何链接发布归属或模拟我发现是SDK的v5中的死链接。
从我可以看到,我需要打电话给/ {user-id} / accounts以从我的用户获取页面的访问令牌,
但是要获得一个 {user-id}
我必须向用户查询,类似于SDK文档中的以下示例:
//确保通过compo加载Facebook SDK for PHP ser或手动
使用Facebook\FacebookRequest;
使用Facebook\GraphUser;
使用Facebook\FacebookRequestException;
if($ session){
try {
$ user_profile =(new FacebookRequest(
$ session,'GET','/ me'
)) - >执行() - > getGraphObject(GraphUser ::的className());
echoName:。 $ user_profile->的getName();
} catch(FacebookRequestException $ e){
echo异常发生,代码:。 $ E->引用代码();
echowith message:。 $ E->的getMessage();
}
这里的问题是我不知道如何获得一个会话需要获取给我的访问令牌的用户数据,以允许我将访问令牌传递到上面的代码片段,这是正确的,这是正确的!?
任何帮助非常感谢!
我在上课,所以我把代码修改为上面的例子。测试和工作代码。
使用您使用的方法获取用户访问令牌后(参见 说明:你必须知道你管理的页面: 然后搜索表以检索 最后,只需运行SDK提供的post函数: p> I have the below snippet and it works and posts to a Facebook page as my own user account on Facebook. The values Now my question is how can I get it to post as the actual page and not my account which is the admin of the page. I've had a look at the SDK documentation and I've been going around in circles, there are many examples of v4 but as it's deprecated I'm trying to use v5 and just can't seem to figure it out, any links to post attribution or impersonation I find are dead links in v5 of the SDK. From what I can see I need to make a call to /{user-id}/accounts to get an access token for the page from my user, https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens But to get a The issue here is that I have no idea how to get a session which I need to get the user data for which gives me the access token to allow me pass the access token into my code snippet above that works, that's if I understand it all correctly!? Any help greatly appreciated! I work with classes, so I adapted my code to your examples above. Tested and working code. After getting your user access token using the method you use (see the guide here), we have to obtain a long-lived access token. Add this to your code : Explanations : You have to know which pages you are administrator : Then search the table to retrieve Finally, simply run the post function provided by the SDK : 这篇关于Facebook SDK v5在墙上贴页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
session_start();
$ helper = $ fb-> getRedirectLoginHelper();
try {
$ accessToken = $ helper-> getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $ e){
//与Graph
echo $ e-> getMessage()通信时发生错误;
退出;
}
if(isset($ accessToken)){
$ client = $ fb-> getOAuth2Client();
try {
$ accessToken = $ client-> getLongLivedAccessToken($ accessToken);
} catch(Facebook\Exceptions\FacebookSDKException $ e){
echo $ e-> getMessage();
退出;
}
$ response = $ fb-> get('/ me / accounts',(string)$ accessToken);
foreach($ response-> getDecodedBody()as $ allPages){
foreach($ allPages as $ page){
if(isset($ page ['id'])&& $ page ['id'] == $ pageId){//假设将其保存为此变量
$ appAccessToken =(string)$ page ['access_token'];
break;
}
}
}
$ response = $ fb-> post(
'/'.$pageId.'/feed',
数组(
message=>消息,
link=>http://www.example.com,
picture=> ;http://www.example.net/images/example.png,
name=>标题,
caption=>www.example.com ,
description=>说明示例
),
$ appAccessToken
);
//成功
$ postId = $ response-> getGraphNode();
echo $ postId;
} elseif($ helper-> getError()){
var_dump($ helper-> getError());
var_dump($ helper-> getErrorCode());
var_dump($ helper-> getErrorReason());
var_dump($ helper-> getErrorDescription());
退出;
}
$ response = $ fb-> get(' me / accounts',(string)$ accessToken);
页面的访问令牌
对我们感兴趣(我已经选择了参考的页面的id)。
$ response = $ fb-> post(
'/'.$pageId.'/feed',
数组(
message=>消息,
link=>http://www.example.com,
picture=> http://www.example.net/images/example.png,
name=>标题,
caption=>www.example.com
description=>描述示例
),
$ appAccessToken
);
FACEBOOK_*
are defined earlier in the codebase.// SDK Version 5.0
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v2.4',
]);
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/'.FACEBOOK_PAGE_ID.'/feed', $postData, FACEBOOK_ACCESS_TOKEN);
$postId = $response->getGraphNode();
{user-id}
I have to query the user, with something like the below example from the SDK documentation:// Make sure to load the Facebook SDK for PHP via composer or manually
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
if($session) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
session_start();
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
if (isset($accessToken)) {
$client = $fb->getOAuth2Client();
try {
$accessToken = $client->getLongLivedAccessToken($accessToken);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo $e->getMessage();
exit;
}
$response = $fb->get('/me/accounts', (string) $accessToken);
foreach ($response->getDecodedBody() as $allPages) {
foreach ($allPages as $page ) {
if (isset($page['id']) && $page['id'] == $pageId) { // Suppose you save it as this variable
$appAccessToken = (string) $page['access_token'];
break;
}
}
}
$response = $fb->post(
'/'.$pageId.'/feed',
array(
"message" => "Message",
"link" => "http://www.example.com",
"picture" => "http://www.example.net/images/example.png",
"name" => "Title",
"caption" => "www.example.com",
"description" => "Description example"
),
$appAccessToken
);
// Success
$postId = $response->getGraphNode();
echo $postId;
} elseif ($helper->getError()) {
var_dump($helper->getError());
var_dump($helper->getErrorCode());
var_dump($helper->getErrorReason());
var_dump($helper->getErrorDescription());
exit;
}
$response = $fb->get('/me/accounts', (string) $accessToken);
the access token of the page
that interests us (I have chosen to take the id of the page referenced).$response = $fb->post(
'/'.$pageId.'/feed',
array(
"message" => "Message",
"link" => "http://www.example.com",
"picture" => "http://www.example.net/images/example.png",
"name" => "Title",
"caption" => "www.example.com",
"description" => "Description example"
),
$appAccessToken
);