Https://api.twitter.com/1.1/statuses/update_with_media.json


#1

I want to update image in status using c#. Here is my code… that returns (400) Bad Request…
Any help will be appreciated…
thanks…

public const string url = "https://api.twitter.com/1.1/statuses/update_with_media.json"; string contentType = fileAttachment.PostedFile.ContentType;
            HttpPostedFile myFile = fileAttachment.PostedFile;
            string fileName = myFile.FileName;

            int nFileLen = myFile.ContentLength;
            byte[] binaryImageData = new byte[nFileLen];
            myFile.InputStream.Read(binaryImageData, 0, nFileLen);

HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = “”;

        webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;

       
            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;
            
            
                webRequest.ContentType = "application/x-www-form-urlencoded";

                if (binaryImageData != null)
                {
                    string boundary = Guid.NewGuid().ToString();
                    string encoding = "iso-8859-1";

                    webRequest.PreAuthenticate = true;
                    webRequest.AllowWriteStreamBuffering = true;
                    webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                    webRequest.Method = "POST";

                    string header = string.Format("--{0}", boundary);
                    string footer = string.Format("--{0}--", boundary);

                    StringBuilder contents = new StringBuilder();
                    contents.AppendLine(header);

                    string fileContentType = ContentType;
                    string fileHeader = String.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", "media", filename);
                    string fileData = Encoding.GetEncoding(encoding).GetString(binaryImageData);

                    contents.AppendLine(fileHeader);
                    contents.AppendLine(String.Format("Content-Type: {0}", fileContentType));
                    contents.AppendLine();
                    contents.AppendLine(fileData);
                    contents.AppendLine(footer);

                    byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(contents.ToString());
                    webRequest.ContentLength = bytes.Length;

                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        requestStream.Write(bytes, 0, bytes.Length);
                    }

                    WebResponse(webRequest);

                    using (var response = (HttpWebResponse)webRequest.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            //Posting Successfull
                        }
                    }

#2

Check out our guide on uploading media over at https://dev.twitter.com/docs/uploading-media


#3

I keep getting something similar. For the signature calculation do you include ‘POST’ and the media URL. Specifically https://dev.twitter.com/discussions/1059
says: “Only the oauth_* parameters are used. (see examples below)”

yet the example basestring is “POST&http%3A%2F%2Fupload.twitter.com%2F1%2Fstatuses%2Fupdate_with_media.json&oauth_consumer_key%3DmbmuCGVFTGHZ” …
I’ve tried both and neither is authenticated.