简体   繁体   中英

Bad Request: Twitter API Upload Image

I have a requirement to post image to Twitter via their REST API. 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. But, on Post, the response is recieved as 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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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