简体   繁体   English

错误的要求:Twitter API上传图片

[英]Bad Request: Twitter API Upload Image

I have a requirement to post image to Twitter via their REST API. 我需要通过其REST API将图像发布到Twitter。 I referred this documentation and came up with below solution to frame the request. 我参考了文档,并提出了以下解决方案来构造请求。 I am creating a multipartformdatacontent object and populating with byte array of the file. 我创建一个multipartformdatacontent对象,并用文件的字节数组填充。 But, on Post, the response is recieved as 400 Bad Request . 但是,在Post上,响应被接收为400 Bad Request What can be the issue here? 这可能是什么问题? Please let me know. 请告诉我。

System.IO.FileStream fileStream = new System.IO.FileStream("C:\\somelocalpath", 
                                      System.IO.FileMode.Open,
                                      System.IO.FileAccess.Read);
byte[] bytearray = new Byte[fileStream.Length];
fileStream.Close();

var multipartContent = new MultipartFormDataContent();
var imageContent = new ByteArrayContent(bytearray);
imageContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
multipartContent.Add(imageContent, "media");

responseTask = await _client.PostAsync("https://upload.twitter.com/1.1/media/upload.json", multipartContent);

PS: I am attaching all authorization params to the request (which work fine on get requests, so should be good here as well) PS:我将所有授权参数附加到请求上(在get请求上可以正常使用,因此在这里也应该很好)

Initialize your byte array like below 如下初始化您的字节数组

byte[] bytearray = File.ReadAllBytes(yout file Name);

Also you may like to set content length before making post call. 另外,您可能希望在拨打电话后设置内容长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM