POST an image on Twitter by status / update_with_media in C #


How post an image to twitter by POST statuses/update_with_media in C#

Ok. I will answer this under the assumption you know how to simply "Tweet" text using their REST api. If you don't, please let me know, and I can help there as well.

The following two methods, obviously, have a lot more to them, but they do show in detail what needs to be additionally done in the REST request. It's important to note that you don't use any query strings in this part of the request

These are methods from an OAuth library I wrote that handles api calls to various REST API's like Twitter and LinkedIn.

The key is how you add the tweet and photo information in the request stream. I must give credit to the individual at this link for his example... it certainly influenced my PrepareTwitterDataRequest() Method. I also realize this example is in VB.NET and you asked for C#... If you can't translate try this.

Friend Function ApiWebRequest(ByVal method As String, ByVal url As String, ByVal fileRequest As OAuthFileRequest) As String

        Dim request As HttpWebRequest = Me.GenerateCoreWebRequest(url, method)

        request.Headers.Add("Authorization", Me.GetAuthHeaderValue(New Uri(url), method))

        Select Case Me.ApiType

            Case OAuthApiType.Twitter
                Me.PrepareTwitterDataRequest(request, fileRequest)

            Case OAuthApiType.LinkedIn
                Me.PrepareLinkedInDataRequest(request, fileRequest)

            Case OAuthApiType.Other
                Me.PrepareDataRequest(request, fileRequest)

        End Select

        Return Me.ProcessWebRequest(request)

    End Function

    Private Sub PrepareTwitterDataRequest(ByRef request As HttpWebRequest, ByVal fileRequest As OAuthFileRequest)

        Dim shortFileName As String = fileRequest.FileShortName

        Dim fileContentType As String = Me.GetMimeType(shortFileName)

        Dim message As String = fileRequest.Message

        Dim encoding As Text.Encoding = Text.Encoding.GetEncoding("iso-8859-1")

        Dim fileHeader As String = String.Format("Content-Disposition: file; " & "name=""media""; filename=""{0}""", shortFileName)

        Dim boundary As String = DateTime.Now.Ticks.ToString("x")

        Dim separator As String = String.Format("--{0}", boundary)

        Dim footer As String = String.Format("\r\n{0}--\r\n", separator)

        Dim contents As New Text.StringBuilder()

        contents.AppendLine("Content-Disposition: form-data; name=""status""")
        contents.AppendLine(String.Format("Content-Type: {0}", fileContentType))

        Dim contentsBuffer As Byte() = encoding.GetBytes(contents.ToString())

        Dim footBuffer As Byte() = encoding.GetBytes(footer)

        'collect all byte() into one stream
        Dim byteCollector As New MemoryStream
        byteCollector.Write(contentsBuffer, 0, contentsBuffer.Length)
        byteCollector.Write(fileRequest.FileData, 0, fileRequest.FileData.Length)
        byteCollector.Write(footBuffer, 0, footBuffer.Length)

        'dump collector stream into byte()
        Dim requestBytes As Byte() = byteCollector.ToArray()

        'close/dispose the collector

        request.ContentType = (String.Format("multipart/form-data; boundary=""{0}""", boundary))
        request.ContentLength = requestBytes.Length

        Using reqStream As IO.Stream = request.GetRequestStream()

            reqStream.Write(requestBytes, 0, requestBytes.Length)

        End Using

    End Sub