2012-01-27 78 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

相關問題