可以创建一个应用程序来上传Facebook上的个人资料图片

时间:2015-09-04 13:40:39

标签: php facebook

有人可以告诉我Facebook是否允许从应用程序上传照片? 我自己设法上传了一张照片和状态但没有个人资料照片。 我很抱歉我的英语不好     DIR。 '/';     require_once($ base_path。'facebook / autoload.php');     使用Facebook \ FacebookSession;     使用Facebook \ FacebookRedirectLoginHelper;

# login-callback.php
$fb = new Facebook\Facebook([
  'app_id' => '879976278753562',
  'app_secret' => '0c348fe777976e07ddbdee8dc2a0fa73',
  'default_graph_version' => 'v2.3',
  ]);

$helper = $fb->getRedirectLoginHelper();
try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
} catch(Exception $e){
    echo $e->getMessage();
    exit;
}

if (!isset($accessToken)) {

      if ($helper->getError()) {
        header('HTTP/1.0 401 Unauthorized');
        echo "Error: " . $helper->getError() . "\n";
        echo "Error Code: " . $helper->getErrorCode() . "\n";
        echo "Error Reason: " . $helper->getErrorReason() . "\n";
        echo "Error Description: " . $helper->getErrorDescription() . "\n";

      } else {

        header('HTTP/1.0 400 Bad Request');
        echo 'Bad request';
    }
    exit;
}

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);

if (! $accessToken->isLongLived()) {
  // Exchanges a short-lived access token for a long-lived one
  try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
    exit;
  }

} else {
    echo '<h3>Suksess</h3>';
}    
$_SESSION['fb_access_token'] = (string) $accessToken;

//$fb->setDefaultAccessToken($accessToken);
$picUrl = "http://images2.fanpop.com/images/photos/5100000/Cute-pups-dogs-5114450-500-309.jpg";

$linkData = [
    'url' => $picUrl,
    'message' => 'Prova 1',
];
try{
    $response = $fb->post('/me/photos',$linkData,$accessToken);
} catch (Facebook\Exceptions\FacebookResponseException $e){
    echo 'Response Exception: ' . $e->getMessage();
    exit;
} catch (Facebook\Exceptions\FacebookSDKException $e){
    echo 'SDK Exception: ' . $e->getMessage();
    exit;
} catch (Exception $e){
    echo 'Unknown Exception: ' . $e->getMessage();
    exit;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
?>

1 个答案:

答案 0 :(得分:0)

您无法通过图谱API直接设置个人资料图片:

  

您无法在此端点上执行此操作。