我做一個測試,以嘗試將照片上傳到用戶的個人資料。我能夠通過將照片發佈到/ me/photos來將照片上傳到默認應用程序相冊;但是,當我嘗試使用相同的方法將另一張照片發佈到同一張專輯時,它會返回OAuthException。當我嘗試上傳到單獨的現有相冊時,也會出現同樣的情況。上傳照片到現有相冊
如果我刪除應用程序的專輯,然後我就可以再次上傳和它的作品,但只要我嘗試上傳其他,它失敗。
我既與.NET SDK,並與標準的PHP SDK嘗試過。兩者都給出了相同的結果。
.NET SDK代碼
這裏是動作我張貼我的形式:
public ActionResult UploadToDefaultAlbum(PhotoUploadModel model)
{
model = model ?? new PhotoUploadModel();
if (model.IsPostback)
{
if (model.PhotoUpload != null)
{
var fb = new FacebookWebClient(model.AccessToken);
dynamic parameters = new ExpandoObject();
parameters.title = model.Title;
parameters.description = model.Title + " description";
parameters.source =
new FacebookMediaObject {ContentType = model.PhotoUpload.ContentType, FileName = model.PhotoUpload.FileName}.
SetValue(GetFileBytes(model.PhotoUpload.InputStream));
var result = fb.Post("/me/photos", parameters);
ViewBag.Message = result;
}
else
{
ViewBag.Message = "PhotoUpload is null";
}
}
model.IsPostback = true;
return View("Upload",model);
}
而我的HTML表單:
@using (Html.BeginForm("UploadToDefaultAlbum", "Photos", FormMethod.Post, new { enctype = "multipart/form-data"})) {
<fieldset>
<legend>Upload to default App album</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Title)
</div>
<div class="editor-field">
@Html.InputFor(System.Web.Mvc.Html5.InputType.Text, x => x.Title)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.PhotoUpload)
</div>
<div class="editor-field">
@Html.InputFor(System.Web.Mvc.Html5.InputType.File, x => x.PhotoUpload)
</div>
@Html.HiddenFor(x => x.IsPostback)
@Html.HiddenFor(x => x.AccessToken)
<button type="submit">Submit</button>
</fieldset>
}
當用戶加載表單頁面時,我確認他們有user_photos和p ublish_stream權限(使用CanvasAuthorize屬性)。
有我丟失的東西,讓我上傳到現有相冊?
PHP SDK代碼
我直接從https://developers.facebook.com/blog/post/498/複製這些代碼,並只需設置變量在頂部有我的應用程序。 ?
<?php
$app_id = "xxx";
$app_secret = "xxx";
$post_login_url = "https://localhost/facebookresearch/photoupload.php";
$album_name = 'PHP Test Album';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//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 = $params['access_token'];
// 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>';
}
>
的迴應是:
{
"error": {
"message": "An unexpected error has occurred. Please retry your request later.",
"type": "OAuthException"
}
}
它創建相冊,但照片不能上傳到它。所以看起來有一個設置我在我的應用程序錯誤或FB中的錯誤。我會猜測前者,但無法弄清楚它可能是什麼。
它會有所不同,該應用程序處於沙盒模式,我正在使用測試用戶?
爲什麼當我嘗試將第二張照片上載到應用程序相冊中的/ me /照片時,會出現同樣的錯誤? – jwynveen 2012-02-03 17:21:42