-7

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() . "\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'];
?>
  • 3
    *Someone can tell me if Facebook allows to upload photos from the application?* - **Yes, it does.** - *I myself have managed to upload a photo together with status but no profile photo.* - **It is mostly unfortunate.**. Please, consider taking the [tour](http://stackoverflow.com/tour) and try to ask your question again after this. – al'ein Sep 04 '15 at 13:48
  • 1
    http://stackoverflow.com/help/on-topic – jmargolisvt Sep 04 '15 at 13:50
  • Can you tell who's permissions to upload profile pictures? This is the code that I'm using https://albapps.net/apps/profile_photo/profil_picture.txt – kadriManaj Sep 04 '15 at 13:54
  • 1
    Edit your question to insert your code there, so everyone can see it. – al'ein Sep 04 '15 at 13:55

1 Answers1

0

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

You can't perform this operation on this endpoint.

Tobi
  • 31,405
  • 8
  • 58
  • 90
  • But you can **upload** a photo and then set it as profile picture. Would not be the direct way, but is a way. – al'ein Sep 04 '15 at 13:54
  • @AedixRhinedale What do you mean exactly? How do you set is as profile picture? Can you link the docs? – Tobi Sep 04 '15 at 13:58
  • @Tobi nope, because I really don't know how to do it anymore than SO questions told me. But I got the thought from [PicBadges](https://www.picbadges.com), where they actually implemented this (thus I believe it's not by direct Graph API callings but by some kind of url redirection to the *crop image* section). Anyway, OP's question AFAIK was *is there a way to do it*, so apparently there is. – al'ein Sep 04 '15 at 14:02
  • Nope, none that I know of. And if you make a statement that it works, please provide the solution via docs or code. It's confusing to just say "It works (somehow, but I don't know...)" – Tobi Sep 04 '15 at 14:04
  • It's a matter of googling, that's why I'm just commenting. Can't do it right now but I thought it was a point worth adding. I think I should *provide complete point base for answers* when I choose to provide a complete *answer*, but it wasn't the case. – al'ein Sep 04 '15 at 14:07
  • Well, you'll leave the author of the question confused then. That's bad practice... – Tobi Sep 04 '15 at 14:09
  • Please can someone tell me who made it? – kadriManaj Sep 04 '15 at 14:12
  • @kadriManaj That's exactly the point. According to the official docs it's not possible. – Tobi Sep 04 '15 at 14:12
  • oh no my bossi will kill me :S there is no artenative way ? – kadriManaj Sep 04 '15 at 14:15
  • Since when *"there is a way, look at those guys, they are doing it, so it's not impossible"* is confusing? And I believe knowing how the did it fits inside **research efforts** definition, and having issues after this fits SOs 'on topic' directives. Sorry, but I can't see the wrong in commenting this. – al'ein Sep 04 '15 at 14:16
  • And I think your answer's mistaken too. Facebook's Graph API docs says it cannot be done **on this endpoint**, meaning there is no direct API implementation to change user's pic. But OP's question was "is there a way to do it", and there is a use case saying it does. That's my whole point. – al'ein Sep 04 '15 at 14:19
  • I won't give my photo complete photo details of my FB account to this obscure app to check this. Quick googling returns nothing valuable after 2010, and the current docs also contain nothing. Furthermore, there can be differences between Graph API versions, so it's possible that they use another version. – Tobi Sep 04 '15 at 14:20
  • can exist in a way to function make a specific picture profile picture that exists in the wall? – kadriManaj Sep 04 '15 at 14:28
  • Try the answer to [this question](http://stackoverflow.com/questions/1648859/can-i-set-a-users-profile-image-using-the-facebook-api) (trying won't kill anybody). It uploads a photo to an album then redirects the user to image cropping part before changing user pic. It makes him open his Facebook interface directly thou, so I think it's not worth it as @Tobi 's answer implies. – al'ein Sep 04 '15 at 14:32
  • Well, that answer is 4 years old, and based on a non-official workaround. Maybe you're lucky and this still works, but I wouldn't recommend a workaround to be part of a productive app... And, if FB finds out about this, it's likely that you get your app blocked. – Tobi Sep 04 '15 at 14:35