Facebook 将照片上载到现有相册
我正在做一个测试,试图上传照片到用户的个人资料。通过将照片发布到/me/photos,我可以让它将照片上传到默认的应用程序相册中;但是,当我尝试使用相同的方法将另一张照片发布到同一相册时,它返回一个OAuthException。当我试图上传到一个单独的现有相册时,也会发生同样的事情 如果我删除了应用程序相册,我可以再次上传它,但一旦我尝试上传另一个,它就会失败 我用.NETSDK和标准PHPSDK都试过了。两者的结果相同 .NET SDK代码 以下是我将表格张贴到的操作:Facebook 将照片上载到现有相册,facebook,facebook-graph-api,facebook-c#-sdk,Facebook,Facebook Graph Api,Facebook C# Sdk,我正在做一个测试,试图上传照片到用户的个人资料。通过将照片发布到/me/photos,我可以让它将照片上传到默认的应用程序相册中;但是,当我尝试使用相同的方法将另一张照片发布到同一相册时,它返回一个OAuthException。当我试图上传到一个单独的现有相册时,也会发生同样的事情 如果我删除了应用程序相册,我可以再次上传它,但一旦我尝试上传另一个,它就会失败 我用.NETSDK和标准PHPSDK都试过了。两者的结果相同 .NET SDK代码 以下是我将表格张贴到的操作: public Acti
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>
它创建相册,但无法将照片上载到相册。因此,我的应用程序中似乎存在设置错误或FB中存在错误。我猜是前者,但不知道可能是什么
应用程序处于沙盒模式,而我使用的是测试用户,这会有区别吗?它既不是fb中的bug,也不是php或csharp sdk中的bug。这是预期的行为
您无法将照片上载到应用程序不拥有的相册。这似乎只是在沙盒模式下使用应用程序时出现的问题。我将我的应用程序从沙盒模式切换出来,一切正常。当我尝试将第二张照片上载到应用程序相册中的/me/photos时,为什么会出现同样的错误?
<?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"
}
}