使用twitter api在Twitter上发布推文

时间:2014-04-24 06:40:53

标签: asp.net vb.net twitter

我想在VB中使用twitter api发布推文 使用以下代码

Dim oauth_token = "aaa-1223d5fd1vfd5v1"
Dim oauth_token_secret = "asd23asd23adv534d3v13d54bs4v3dzb"
Dim oauth_consumer_key = "651sd313d4"
Dim oauth_consumer_secret = "dcs53sd123131fvb53215cv21321dv31dv3d1vsadf"

'==========================

Dim strURL = "https://api.twitter.com/1.1/statuses/update.json"

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()

Dim oauth_signature As String = GeneraOAuthSignature(strURL, _
                                    oauth_nonce, _
                                    oauth_signature_method, _
                                    oauth_timestamp, _
                                    oauth_version)

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 strText As String = "@PramodNDS MyTweet"
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))

ServicePointManager.Expect100Continue = False

Dim req As WebRequest
Dim res As HttpWebResponse
Dim streamReader As StreamReader
Dim contenidoRespuesta As String = ""
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

req = WebRequest.Create(strURL)
req.Timeout = -1
req.Headers.Add("Authorization", authHeader)

res = DirectCast(req.GetResponse(), HttpWebResponse)

'==========================

streamReader = New StreamReader(res.GetResponseStream(), encode)
While True
    contenidoRespuesta = streamReader.ReadLine()
    If Not String.IsNullOrEmpty(contenidoRespuesta) Then
        InsertFacebookFeeds(contenidoRespuesta)
        Exit While
    End If

End While

'==========================

'Abort is needed or streamReader.Close() will hang
req.Abort()
streamReader.Close()
streamReader = Nothing
res.Close()
res = Nothing

如何使用此代码发布推文来获取特定的推文ID或screen_name。

我尝试过以下代码

objTwitter = New TwitterVB2.TwitterOAuth("jcLWDSpR587E0jIVtcmbcg", "VhgnHiITdBetYNDhMxJ29ObptqLWdGvraKG6qb4N6uY", "2325327612-MCOa5ZfhJC95LqoKcmflkoOBFh70wbq66g0Gwax", "MmBRZKscHJWnzb7Pn4Up2j0YeHCtYucV8DIiAOWhtrUvg")

 If strStatusMessage.Length < 140 Then
                        strStatusMessage = HttpContext.Current.Server.UrlEncode(strStatusMessage)
                        strStatusMessage = strStatusMessage.Replace("*", "%2A").Replace("+", "%20")
                        Dim urii As Uri = New Uri(String.Format("https://api.twitter.com/1.1/statuses/update.json?status={0}", strStatusMessage))
                        Dim signature As String = objTwitter.GenerateSignature(urii, "01dbDUsbNvfcMHFjlXCJbw", "DCwZbwQ9kxEMBZ9geqYuNLGxnIWR6JHv0yfckPX14", authtoken, tokensecret, OAuth.Method.POST, objTwitter.GenerateTimeStamp(), objTwitter.GenerateNonce(), outsigurl, outsigpar, DBNull.Value.ToString, "")
                        xml = objTwitter.OAuthWebRequest(OAuth.Method.POST, outsigurl, outsigpar)
  End If

它给出了TwitterAPI的例外。

任何帮助和建议都会有用。

0 个答案:

没有答案