2012-01-27 30 views
1

我在Facebook上有一个应用程序,我可以选择一张图片,然后上传到与该应用程序相关的专辑。在我的应用程序的相册中使用Facebook api发布图片

我发现了一些关于它的代码,但到现在为止,我只能上传到我的个人帐户。

有了这个代码,我可以获取应用程序的所有专辑细节:

$facebook->api('/app_id/albums?access_token='.$acces_token); 

这很酷,我得到的名单,但在此之后,我执行以下操作:

$upload_photo = $facebook->api('/'.$album_id.'/photos?access_token='.$acces_token, 'post', $photo_details); 

而且我总是得到以下豁免: 致命错误:未捕获的OAuthException:(#100)专辑所有者抛出的ID无效ID

什么问题? 也许我没有正确的权限?或者我错过了什么?

我甚至试过这样:

$create_album = $facebook->api('/app_id/albums?access_token='.$acces, 'post', $album_details); 
$album_uid = $create_album['id']; 
$upload_photo = $facebook->api('/'.$album_uid.'/photos?access_token='.$acces, 'post', $photo_details); 
+0

嗨安德拉斯,你能够做到这一点?我现在正在尝试做同样的事情。谢谢! – 2014-03-12 21:21:41

+0

嗨,你有没有找到错误的原因?我也坚持它.. 我无法确定..有太多access_tokens。 1.为应用程序,1为页面.. – Prashant 2014-06-10 16:01:16

回答

0

试试这个!它创建一个新的相册,然后它张贴在该特定专辑的照片。

$post_login_url = "http://apps.facebook.com/yourapp/thefile"; 
    $album_name = 'name of Album'; 
    $album_description = 'something about album'; 


    $code = $_REQUEST["code"]; 
$facebook->setFileUploadSupport(true); 
    //Obtain the access_token with publish_stream permission 
    if(empty($code)) 
    { 
     $dialog_url= "http://www.facebook.com/dialog/oauth?" 
     . "client_id=" . $app_id 
     . "&redirect_uri=" . urlencode($post_login_url) 
     . "&scope=publish_stream"; 
     echo("<script>top.location.href='" . $dialog_url . 
     "'</script>"); 
    } 
    else { 
    $token_url= "https://graph.facebook.com/oauth/" 
    . "access_token?" 
    . "client_id=" . $app_id 
    . "&redirect_uri=" . urlencode($post_login_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code; 
    $response = file_get_contents($token_url); 
    $params = null; 
    parse_str($response, $params); 
    $access_token = $facebook->getAccessToken(); 

    // Create a new album 
    $graph_url = "https://graph.facebook.com/me/albums?" 
    . "access_token=". $access_token; 

    $postdata = http_build_query(
    array(
     'name' => $album_name, 
     'message' => $album_description 
     ) 
    ); 
    $opts = array('http' => 
    array(
     'method'=> 'POST', 
     'header'=> 
     'Content-type: application/x-www-form-urlencoded', 
     'content' => $postdata 
    ) 
    ); 
    $context = stream_context_create($opts); 
    $result = json_decode(file_get_contents($graph_url, false, 
     $context)); 

    // Get the new album ID 
    $album_id = $result->id; 

    //Show photo upload form and post to the Graph URL 
    $graph_url = "https://graph.facebook.com/". $album_id 
     . "/photos?access_token=" . $access_token; 
    echo '<html><body>'; 
    echo '<form enctype="multipart/form-data" action="' 
    .$graph_url. ' "method="POST">'; 
    echo 'Adding photo to album: ' . $album_name .'<br/><br/>'; 
    echo 'Please choose a photo: '; 
    echo '<input name="source" type="file"><br/><br/>'; 
    echo 'Say something about this photo: '; 
    echo '<input name="message" type="text" 
     value=""><br/><br/>'; 
    echo '<input type="submit" value="Upload" /><br/>'; 
    echo '</form>'; 
    echo '</body></html>'; 
    } 

?> 
+1

Thx,但它没有做我想做的,这将创建它在我的PERSONAL页面上,我想要的是在应用程序页面上创建它:) – ghostika 2012-01-28 23:30:43

相关问题