我已经获得了使用OAuth上传的以下代码。
public async Task UploadPhotoContinueAsync(IEnumerable<StorageFile> files)
{
var uri = string.Format("https://api.500px.com/v1/photos/upload?name={0}&description={1}&privacy=0&category=0", "test name", "test description");
var backgroudUploader = new BackgroundUploader();
var headers = OAuthUtility.BuildBasicParameters(consumerKey, consumerSecret, uri, HttpMethod.Post, GetAccessToken());
var header = string.Empty;
var file = files.First();
foreach (var item in headers)
{
header += string.Format(@"{0}=""{1}"",", item.Key, item.Value);
}
header = header.Remove(header.Length - 1);
backgroudUploader.SetRequestHeader("Authorization", string.Format("OAuth {0}", header));
backgroudUploader.SetRequestHeader("Filename", file.Name);
var op = backgroudUploader.CreateUpload(new Uri(uri), file);
try
{
var result = await op.StartAsync();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
问题是每次我开始操作时都会从API中获得内部服务器错误。
我似乎无法弄清问题是什么。
有人想出来了吗?感谢。
编辑:
我终于让Fiddler工作了。以下是应用发送给API的标头:
答案 0 :(得分:0)
我注意到Fiddler版本中没有内容类型。看起来像500px需要多部分内容类型,并且从twitter发布的控制台屏幕显示正在设置。
您是否尝试使用BackgroundUploader.SetRequestHeader强制内容类型?
backgroundUploader.SetRequestHeader(&#34; Content-Type&#34;,&#34; multipart / form-data; boundary =&#34; + boundray); backgroundUploader.Method =&#34; POST&#34 ;;
如果失败,可以使用BackgroundTransferContentPart? http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.backgroundtransfer.backgroundtransfercontentpart.aspx
答案 1 :(得分:0)
我知道它不是很专业,但我终于找到了修改代码的方法:)
这里有更新的功能:
public async Task UploadPhotoContinueAsync(IEnumerable<StorageFile> files)
{
var uri = string.Format("https://api.500px.com/v1/photos/upload?name={0}&description={1}&privacy=0&category=0", "test name", "test description");
var backgroudUploader = new BackgroundUploader();
var headers = OAuthUtility.BuildBasicParameters(consumerKey, consumerSecret, uri, HttpMethod.Post, GetAccessToken());
var header = string.Empty;
var file = files.First();
foreach (var item in headers)
{
header += string.Format(@"{0}=""{1}"", ", item.Key, item.Value);
}
header = header.Remove(header.Length - 2);
backgroudUploader.SetRequestHeader("Authorization", string.Format("OAuth {0}", header));
backgroudUploader.SetRequestHeader("Filename", file.Name);
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
backgroudUploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
backgroudUploader.Method = "POST";
var parts = new List<BackgroundTransferContentPart>();
var part = new BackgroundTransferContentPart();
part.SetHeader("Content-Disposition", @"form-data; name=""file""; filename=""" + file.Name + "\"");
part.SetFile(file);
parts.Add(part);
var op = await backgroudUploader.CreateUploadAsync(new Uri(uri), parts, "", boundary);
try
{
var result = await op.StartAsync();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
感谢所有帮助过的人!