Facebook PHP SDK-如何获取访问令牌?
我正试图从我的应用程序在用户的Facebook墙上发帖。用户授予了应用程序在其墙上发布的权限,我在db中有userid。我需要自动发送帖子,而无需用户再次登录 我的代码是:Facebook PHP SDK-如何获取访问令牌?,php,facebook,facebook-graph-api,Php,Facebook,Facebook Graph Api,我正试图从我的应用程序在用户的Facebook墙上发帖。用户授予了应用程序在其墙上发布的权限,我在db中有userid。我需要自动发送帖子,而无需用户再次登录 我的代码是: try{ require_once(dirname(__FILE__) . '\lib\facebook-php\src\facebook.php' ); } catch(Exception $o){ print_r($o); } $config = array( 'appId' =>
try{
require_once(dirname(__FILE__) . '\lib\facebook-php\src\facebook.php' );
}
catch(Exception $o){
print_r($o);
}
$config = array(
'appId' => '123456',
'secret' => '454544',
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'access_token' => $facebook->getAccessToken(),
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
// Give the user a logout link
echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
var_dump($e);
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please <a href="' . $login_url . '">login.</a>';
}
试试看{
需要_一次(dirname(u文件_u)。'\lib\facebook php\src\facebook.php');
}
捕获(例外$o){
印刷费($o);
}
$config=array(
“appId”=>“123456”,
“机密”=>“454544”,
'allowSignedRequest'=>false//可选,但对于非画布应用程序,应设置为false
);
$facebook=newfacebook($config);
$user_id=$facebook->getUser();
$user_id='123456';
if($user\u id){
//我们有一个用户ID,所以可能是一个登录的用户。
//如果没有,我们将得到一个异常,我们将在下面处理它。
试一试{
$ret_obj=$facebook->api('/me/feed',POST',
排列(
“访问令牌”=>$facebook->getAccessToken(),
'链接'=>'www.example.com',
'消息'=>'使用PHP SDK发布!'
));
回显“Post ID:”.$ret_obj['ID']。”;
ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
//给用户一个注销链接
回声“
”;
ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
}捕获(FacebookApiException$e){
//如果用户已注销,您可以使用
//用户ID,即使访问令牌无效。
//在这种情况下,我们将得到一个异常,因此
//只需让用户在此处再次登录即可。
var_dump($e);
$login\u url=$facebook->getLoginUrl(数组)(
'范围'=>'发布\u流'
));
回音“请”;
ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
错误日志($e->getType());
错误日志($e->getMessage());
}
}否则{
//没有用户,因此打印一个链接供用户登录
//要发布到用户的墙,我们需要发布流权限
//我们将使用当前URL作为重定向uri,因此我们不会
//需要在这里指定它。
$login\u url=$facebook->getLoginUrl(数组('scope'=>'publish\u stream');
回音“请”;
ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
}
这将抛出一个活动的访问令牌,该令牌必须用于查询当前用户的信息。错误并要求登录。单击“登录”后,帖子成功发布
如何在用户未登录的情况下使用userid获取访问令牌?您必须获得权限。 看看这里
当调用API端点
/me
有两种方法可以解决这个问题
- 使用userid而不是/me
- 在发送api调用之前设置访问令牌
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
您不需要发送'access\u token'=>$facebook->getAccessToken(),
第二种选择是按以下方式操作:
注意:publish\u流
scope必须在登录时发送
弃用通知:
publish\u-stream
已被publish\u-actions
替换,请查看facebook文档中的内容我已在网站上实现facebook登录。用户已授予应用程序在其墙上发布的权限。我需要任何其他权限吗?在您的登录URL中,您必须在范围中传递权限检查我的登录URL的此代码<代码>$login\u url=$facebook->getLoginUrl(数组('scope'=>'photo\u upload,read\u mailbox')代码>可能是您需要访问应用程序中的发布操作权限。所以它就像$login\u url=$facebook->getLoginUrl(数组('scope'=>'public\u actions')代码>版本说明:以上代码已替换为SDK V4。