开发者

facebook upload photo

开发者 https://www.devze.com 2023-02-07 10:47 出处:网络
I\'m new to the facebook api, and i have a small issue. I\'m trying to upload a picture to an album of an user. For this i\'m using a PHP script i\'ve found here, on stack overflow :

I'm new to the facebook api, and i have a small issue. I'm trying to upload a picture to an album of an user. For this i'm using a PHP script i've found here, on stack overflow :

    $app_id = xxx;
$app_secret = "xxx";
$my_url = "http://apps.facebook.com/myapp/test.php";

$code = $_REQUEST["code"];

if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($my_url);

    echo("<script> top.location.href='" . $dialog_url . "'</script>");
}


$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
    . $app_secret . "&code=" . $code;

$access_token = file_get_contents($token_url);

//upload photo
$file= 'test.png';
$args = array(
   'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);

print_r($args);

$ch = curl_init();
$url = 'https://graph.facebook.com/203开发者_运维问答8278/photos?access_token='.$access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
print_r(json_decode($data,true));

But the result is :

Array ( [message] => Photo from application [test.png] => @/[mypath]/test.png ) Array ( [error] => Array ( [type] => OAuthException [message] => Error validating application. ) )

What does this mean ? Do i need to have extended permissions for my app ?wich of them ? i gave it acces to basic and user_photos

Thank you very much!


the second script

$app_id = "xxx"; $app_secret = "xxx"; $my_url = "http://apps.facebook.com/myapp/test.php";

$code = $_REQUEST["code"];

if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=user_photos" ;

    echo("<script> top.location.href='" . $dialog_url . "'</script>");
}


$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
    . $app_secret . "&code=" . $code;

$access_token = file_get_contents($token_url);









$token = $access_token;

//upload photo
$file= 'test.png';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);

$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);

print_r(json_decode($data,true));


I think you need your application to ask for the permission called publish_stream. There is also another permission called photo_upload, but as I understand that is considered old (but may still work)


my dear when ever you upload any photo from your application to facebook, Facebook automatically create an album with the name of your application. You dont need to do any thing. Just upload any photo from your application and then goes to your profile, see all albums then you will see that facebook has created an album with the name of your application and your uploaded photo is residing in it.


I would not go that way if i were you. The cURL method has proven to be, for me at least, a tad too complicated. These are the steps that has to be taken, below you can see a link to a great recent blog post about this exact issue:

  1. Get user's user_photos and publish_stream (not necessary, but good for future development).
  2. Create album on behalf of user, which will contain the photo you wish to upload.
  3. Get the album id of the album you just created.
  4. Specify the photo's parameters: The message to go along with it, the location of the file on the server, and a bunch of other stuff you can look up in this docs reference about publishing photos (bottom of page).
  5. Upload the photo with the given parameters.

Dai Pratt's excellent tutorial on photo uploading via the New Graph API

IMPORTANT NOTE: There is a serious flaw in the script above-it does not check if the album exists already, in case you want to upload multiple photos on behalf of the user. It's important you store the user's id and the album id you created in a database, so you can query it each time the user wishes o upload an image, and create or not create an album accordingly.

EDIT: And now, after I've posted, I see the exact link in a comment here. Anyhow-hope this helps:)

0

精彩评论

暂无评论...
验证码 换一张
取 消