尝试将照片上传到flickr时遇到问题。 我有我的访问令牌,我知道它正在运行,因为我可以毫无问题地调用 flickr.people.getUploadStatus 。 因为我可以调用该方法,所以我已经验证我尝试上传的文件符合特定的配额(即文件小于maxbytes。 无论如何,我上传的代码如下所示:
public string UploadToApi(Dictionary<string, string> parameters, string fileName)
{
var apiUrl = BuildUrl(parameters, "http://api.flickr.com/services/upload/");
var baseParameters = GetParameters(new Dictionary<string, string>() { { "oauth_token", accessKey } });
var signature = OAuthProvider.GenerateSignature(new Uri(apiUrl), baseParameters, consumerSecret, accessSecret, "POST");
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
var request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.ServicePoint.Expect100Continue = false;
var dictionary = baseParameters;
dictionary.Add("oauth_signature", signature);
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
foreach (var item in dictionary)
{
writer.WriteLine("--" + boundary);
writer.WriteLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", item.Key));
writer.WriteLine();
writer.WriteLine(item.Value);
}
writer.WriteLine("--" + boundary);
writer.WriteLine("Content-Disposition: form-data; name=\"photo\"; filename=\"" + fileName + "\"");
writer.WriteLine("Content-Type: image/png");
writer.WriteLine();
writer.Flush();
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[4096];
var bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
stream.Write(buffer, 0, bytesRead);
}
}
writer.WriteLine();
writer.WriteLine(boundary + "--");
}
using (var response = request.GetResponse())
{
var httpResponse = (HttpWebResponse)response;
using (var data = response.GetResponseStream())
{
using (var reader = new StreamReader(data))
{
return reader.ReadToEnd();
}
}
}
}
我得到的回复是 signature_invalid 但是当我比较 debug_sbs 时,我发现基本字符串与我生成的基本字符串完全相同。
POST&http%3A%2F%2Fapi.flickr.com%2Fservices%2Fupload%2F&oauth_consumer_key%3D<key>%26oauth_nonce%3D56372bb9-3d98-4009-b03e-981a36345e17%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1372442086%26oauth_token%3D<token>%26oauth_version%3D1.0
另外,为了确保我创建了正确的签名,我使用了http://hueniverse.com/oauth/guide/authentication/并在我的详细信息中输入了它,并且它与完全相同的UrlEncoded签名一起出现....
使用fiddler这是我的帖子:
POST http://api.flickr.com/services/upload/ HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------8d0424817e13cb4
Host: api.flickr.com
Content-Length: 29993
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_consumer_key"
<my consumer key>
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_nonce"
56372bb9-3d98-4009-b03e-981a36345e17
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_timestamp"
1372442086
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_signature_method"
HMAC-SHA1
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_version"
1.0
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_token"
<my oauth token>
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="oauth_signature"
XgJKthsbl03WT0ucespIWhmLNQw%3D
-----------------------------8d0424817e13cb4
Content-Disposition: form-data; name="photo"; filename="C:\Users\r3plica\Dropbox\Projects\Skipstone\Code\Skipstone\Skipstone\Public\Uploads\1637376f-dab6-4944-be5f-53b2603f33c1\0\558c0a8b-8db0-4dba-8481-25a96f9b9f21.png"
Content-Type: image/png
<binary data here>
---------------------------8d0424817e13cb4--
任何人都可以解释我做错了什么吗?
干杯, / r3plica