douyinliu8813
2015-09-04 13:40
采纳率: 100%
浏览 34
已采纳

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

Someone can tell me if Facebook allows to upload photos from the application? I myself have managed to upload a photo together with status but no profile photo. I am sorry for my bad English DIR . '/'; require_once($base_path . 'facebook/autoload.php'); use Facebook\FacebookSession; use 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() . "
";
        echo "Error Code: " . $helper->getErrorCode() . "
";
        echo "Error Reason: " . $helper->getErrorReason() . "
";
        echo "Error Description: " . $helper->getErrorDescription() . "
";

      } 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>

";
    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条回答 默认 最新

  • dongyi2083 2015-09-04 13:50
    已采纳

    You can't directly set the profile picture via the Graph API:

    You can't perform this operation on this endpoint.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题