Twitter上传/媒体追加

时间:2016-04-18 08:21:09

标签: vb.net twitter httpwebrequest

我无法理解如何执行追加步骤。我已经毫无困难地完成了INIT部分,但我不明白如何进行下一步。

我现在正在做什么:我只构建一个只有oauth参数和签名库的授权标题,然后我将所需的参数放入body请求中:command = APPEND,来自INIT方法的media_id,带有的媒体原始二进制图像和segment_index = 0因为我必须只发送一个请求,因为我的图像大小永远不会超过30 kB。

我尝试了以下内容:

        Dim oHttpWebRequest As HttpWebRequest
        Dim oHttpWebResponse As HttpWebResponse
        Dim oStream As Stream
        Dim newLine As String = System.Environment.NewLine
        Dim mediaId = getINITresponse()
        Try
            oHttpWebRequest = DirectCast(WebRequest.Create("https://upload.twitter.com/1.1/media/upload.json"), HttpWebRequest)

            Dim sBody As String = buildHeader(twitter, sAuth)
            oHttpWebRequest.Headers.Add("Authorization", sBody)
            oHttpWebRequest.Method = "POST"

            Dim sBoundary As String = DateTime.Now.Ticks.ToString("x")
            Dim sStartBoundary As String = "--" + sBoundary + newLine
            Dim sEndBoundary As String = newLine + "--" + sBoundary + "--" + newLine

            oHttpWebRequest.ContentType = "multipart/form-data; boundary=" + sBoundary

            Dim sBodyData As String = ""
            Dim srequestString As New StringBuilder()
            sBodyData = "command=APPEND&media_id=" + mediaId + "&segment_index=0&media="
            srequestString.AppendLine(sStartBoundary + "Content-Disposition: form-data; name=""media""; filename=""image.jpg""" + newLine)
            srequestString.AppendLine(Encoding.Default.GetString(getBinaryFileFromPath(sFicName)))
            srequestString.AppendLine(newLine + sEndBoundary + newLine)

            Dim postData As Byte()
            postData = Text.Encoding.UTF8.GetBytes(sBodyData)
            Dim postDataBytes As Byte()
            postDataBytes = Text.Encoding.UTF8.GetBytes(srequestString.ToString()) 
            Dim byteResult() As Byte = postData.Concat(postDataBytes).ToArray()   

            oHttpWebRequest.ContentLength = byteResult.Length
            Using requestStream = oHttpWebRequest.GetRequestStream()
                requestStream.Write(byteResult, 0, byteResult.Length)
                requestStream.Close()
            End Using

            oHttpWebResponse = DirectCast(oHttpWebRequest.GetResponse(), HttpWebResponse)
            oStream = oHttpWebResponse.GetResponseStream()

            Return New StreamReader(oStream, Encoding.UTF8).ReadToEnd()

        Catch wex As WebException 'Exception venant du serveur distant
            Return New StreamReader(wex.Response.GetResponseStream()).ReadToEnd()
        End Try

我的初始化部分正在工作,我保存了media_id并将其用于附加部分。我的图像不超过30 kB,所以这不是尺寸问题。

Twitter回复我的状态:400 Bad Request。

我不知道我做错了什么。抱歉我的英文不好!

1 个答案:

答案 0 :(得分:1)

我找到了答案:

sBodyData = "command=APPEND&media_id=" + mediaId + "&segment_index=0&media="

我正在制作我的正文数据,好像我发送了一个ContentType = application / x-www-form-urlencoded的请求。  所以我在同一个请求中使用了两个contentType,当然它不起作用。现在它看起来像是:

Dim newLine As String = System.Environment.NewLine

srequestString.AppendLine(sStartBoundary + "Content-Disposition: form-data; name=""command""" + newLine)
srequestString.AppendLine("APPEND")
srequestString.AppendLine(sStartBoundary + "Content-Disposition: form-data; name=""media_id""" + newLine)
srequestString.AppendLine(mediaId)
srequestString.AppendLine(sStartBoundary + "Content-Disposition: form-data; name=""media""" + newLine)
srequestString.AppendLine(Encoding.Default.GetString(getBinaryFileFromPath(sFileName)))
srequestString.AppendLine(sStartBoundary + "Content-Disposition: form-data; name=""segment_index""" + newLine)
srequestString.AppendLine("0")
srequestString.AppendLine(sEndBoundary)