0
我必须通过Twitter API 1.1上传头像,但我总是得到422错误。 这是我的代码:twitter帐户/ update_profile_image 422错误
var path = "avatar.jpg";
var req = token.GetQueryWebRequest("https://api.twitter.com/1.1/account/update_profile_image.json", HttpMethod.POST);
ServicePointManager.Expect100Continue = false; /// ????????????
req.ContentType = "multipart/form-data";
var image = "image=" + Uri.EscapeDataString(getImageBase64Encode(path, 700));
var encode = Encoding.ASCII.GetBytes(image);
req.ContentLength = encode.Length;
var steam = req.GetRequestStream();
steam.Write(encode, 0, encode.Length);
var resp = req.GetResponse();
// ----------------
private string getImageBase64Encode(string filePath, int maxSize)
{
if (!File.Exists(filePath))
throw new Exception(string.Format("Файл не существует: {0}", filePath));
var file = new FileInfo(filePath);
if(file.Length > maxSize * 1024)
throw new Exception(string.Format("Файл слишком большой: {0}", filePath));
byte[] res = null;
try
{
res = File.ReadAllBytes(filePath);
}
catch (Exception)
{
throw new Exception(string.Format("Неудалось прочитать файл: {0}", filePath));
}
if (res == null)
{
throw new Exception(string.Format("Файл пуст или поврежден: {0}", filePath));
}
return Convert.ToBase64String(res);
}
GetQueryWebRequest - 获得通过OAuth头请求
不提供框架
PS>对不起,我的英语那么差