我做一个测试,以尝试将照片上传到用户的个人资料。我能够通过将照片发布到/ 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