我的php代码会将您登录到Facebook以成功授予权限。然后它会检索您的个人资料图片和名称。我还试图运行一个脚本,允许用户将消息发布到他的时间轴。目前它运行错误代码200,我不知道如何处理这个问题。
以下是我的申请http://www.vle.co.nz/fb/的链接,我的代码如下。
<?php
require_once('src/Facebook/Entities/AccessToken.php');
require_once('src/Facebook/HttpClients/FacebookHttpable.php');
require_once('src/Facebook/HttpClients/FacebookCurl.php');
require_once('src/Facebook/HttpClients/FacebookCurlHttpClient.php');
require_once('src/Facebook/FacebookSDKException.php');
require_once('src/Facebook/FacebookRequestException.php');
require_once('src/Facebook/FacebookPermissionException.php' );
require_once('src/Facebook/FacebookAuthorizationException.php');
require_once('src/Facebook/FacebookRedirectLoginHelper.php');
require_once('src/Facebook/FacebookRequest.php');
require_once('src/Facebook/FacebookResponse.php');
require_once('src/Facebook/FacebookSession.php');
require_once('src/Facebook/GraphObject.php');
require_once('src/Facebook/GraphSessionInfo.php');
require_once('src/Facebook/GraphUser.php');
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookHttpable;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookSession;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;
try {
session_start();
$app_id = '1619082794989827';
$app_secret = '3b9c9c607ab5529561bdac8c5944a9bb';
$redirect_url = 'http://www.vle.co.nz/fb/';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper->getSessionFromRedirect();
if(isset($sess)) {
$accessToken = $sess->getAccessToken();
$_SESSION['fb_token'] = $accessToken->extend();
$request = new FacebookRequest($sess, 'GET', '/me');
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::classname());
$name = $graph->getName();
$id = $graph->getId();
$image = 'http://graph.facebook.com/'.$id.'/picture?width=300';
echo "<img src='$image' /><br/>";
echo "Hello $name <br>";
echo "Email: $email <br>";
echo "Your facebook ID: $id <br>";
$postRequest = new FacebookRequest($sess, 'POST', '/me/feed', array('message' => 'My First Post using facebook sdk!'));
$postResponse = $postRequest->execute();
$posting = $postResponse->getGraphObject();
echo $posting->getProperty('id');
} else {
echo "<a href='".$helper->getLoginUrl(array('email','user_about_me'))."'>Login with facebook</a>";
}
} catch(FacebookRequestException $e) {
echo "Error failed: " . $e->getCode() ;
}
?>
答案 0 :(得分:0)
要发布到用户的时间表,您需要publish_actions
权限。
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/feed#publish
您只需要email
和user_about_me
echo "<a href='".$helper->getLoginUrl(array('email','user_about_me'))."'>Login with facebook</a>";
API错误200是权限错误。