在vb.net中将图像发布到Twitter

时间:2018-12-07 20:09:13

标签: vb.net twitter

我有将在vb.net中鸣叫文本的代码。我希望能够添加图像。如果需要先上传图片,并且他们给了我一些数字代码,也可以用作参考。我将张贴我用来鸣叫文本以显示我所拥有的代码。.但是我很难找出如何在其中添加图像。

 Public Shared Function Tweet(status As String) As String
        Dim mError As String = String.Empty

        Dim boolResult As Boolean = False
        Dim oauth_version As String = "1.0"
        Dim oauth_signature_method As String = "HMAC-SHA1"
        Dim oauth_nonce As String = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()))
        Dim timeSpan As TimeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
        Dim oauth_timestamp As String = Convert.ToInt64(timeSpan.TotalSeconds).ToString()

        'the 1.1 below my change.. so if it stops working check the url below 
        Dim resource_url As String = "https://api.twitter.com/1.1/statuses/update.json"




        Dim oauth_consumer_key As String = "xxxxxxxxxxxxxxxxxxxxx"
        Dim oauth_consumer_secret As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        Dim oauth_token As String = "xxxxxxxxxxxxxxxxxxxxxxxx"
        Dim oauth_token_secret As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

        Dim baseFormat As String = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}"

        Dim baseString As String = String.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version, Uri.EscapeDataString(status))

        Try
            baseString = String.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString))
            Dim compositeKey = String.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", Uri.EscapeDataString(oauth_token_secret))

            Dim objHasher As HMACSHA1 = New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))

            Dim oauth_signature As String = Convert.ToBase64String(objHasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))

            Dim headerFormat As String = "OAuth oauth_nonce=""{0}"", oauth_signature_method=""{1}"", oauth_timestamp=""{2}"", oauth_consumer_key=""{3}"", oauth_token=""{4}"", oauth_signature=""{5}"", oauth_version=""{6}"""

            Dim authHeader As String = String.Format(headerFormat, Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauth_consumer_key), _
                        Uri.EscapeDataString(oauth_token), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version))

            Dim postBody As String = "status=" + Uri.EscapeDataString(status)

            ServicePointManager.Expect100Continue = False

            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(resource_url), HttpWebRequest)
            request.Headers.Add("Authorization", authHeader)
            request.Method = "POST"
            request.ContentType = "application/x-www-form-urlencoded"

            Dim stream As Stream = request.GetRequestStream()

            Dim contentData() As Byte = ASCIIEncoding.ASCII.GetBytes(postBody)
            stream.Write(contentData, 0, contentData.Length)

            Dim response As WebResponse = request.GetResponse()

            boolResult = True

        Catch ex As Exception
            boolResult = False
            HttpContext.Current.Session.Add("Error", ex.ToString())
            mError = ex.Message.ToString
        End Try

        Return mError
    End Function

0 个答案:

没有答案