我想在我的.net網頁裏面調用流式API過濾器。從vb.net網頁調用Twitter流媒體API(狀態/過濾器)
我確實創造了在Twitter的應用程序,並把所需要的憑據(用戶密鑰,消費者的祕密,訪問令牌和訪問令牌祕密)
這是我使用
Public Function StreamAPI()
Dim url As String = "https://stream.twitter.com/1.1/statuses/filter.json?track=example.com/example"
Dim oauthconsumerkey As String = "consumerkey"
Dim oauthconsumersecret As String = "consumersecret"
Dim oauthsignaturemethod As String = "HMAC-SHA1"
Dim oauthversion As String = "1.0"
Dim oauthtoken As String = "token"
Dim oauthtokensecret As String = "tokenkey"
Dim oauthnonce 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 oauthtimestamp As String = Convert.ToInt64(timeSpan.TotalSeconds).ToString()
Dim basestringParameters As New SortedDictionary(Of String, String)()
basestringParameters.Add("track", "example.com/example")
basestringParameters.Add("oauth_version", oauthversion)
basestringParameters.Add("oauth_consumer_key", oauthconsumerkey)
basestringParameters.Add("oauth_nonce", oauthnonce)
basestringParameters.Add("oauth_signature_method", oauthsignaturemethod)
basestringParameters.Add("oauth_timestamp", oauthtimestamp)
basestringParameters.Add("oauth_token", oauthtoken)
Dim baseString As New StringBuilder()
baseString.Append("GET" + "&")
baseString.Append(EncodeCharacters(Uri.EscapeDataString(url.Split("?"c)(0)) + "&"))
For Each entry As KeyValuePair(Of String, String) In basestringParameters
baseString.Append(EncodeCharacters(Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&")))
Next
Dim finalBaseString As String = baseString.ToString().Substring(0, baseString.Length - 3)
Dim signingKey As String = EncodeCharacters(Uri.EscapeDataString(oauthconsumersecret)) + "&" + EncodeCharacters(Uri.EscapeDataString(oauthtokensecret))
'Sign the request
Dim hasher As New HMACSHA1(New ASCIIEncoding().GetBytes(signingKey))
Dim oauthsignature As String = Convert.ToBase64String(hasher.ComputeHash(New ASCIIEncoding().GetBytes(finalBaseString)))
Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim authorizationHeaderParams As New StringBuilder()
authorizationHeaderParams.Append("OAuth ")
authorizationHeaderParams.Append("oauth_nonce=" + """" + Uri.EscapeDataString(oauthnonce) + """,")
authorizationHeaderParams.Append("oauth_signature_method=" + """" + Uri.EscapeDataString(oauthsignaturemethod) + """,")
authorizationHeaderParams.Append("oauth_timestamp=" + """" + Uri.EscapeDataString(oauthtimestamp) + """,")
authorizationHeaderParams.Append("oauth_consumer_key=" + """" + Uri.EscapeDataString(oauthconsumerkey) + """,")
If Not String.IsNullOrEmpty(oauthtoken) Then
authorizationHeaderParams.Append("oauth_token=" + """" + Uri.EscapeDataString(oauthtoken) + """,")
End If
authorizationHeaderParams.Append("oauth_signature=" + """" + Uri.EscapeDataString(oauthsignature) + """,")
authorizationHeaderParams.Append("oauth_version=" + """" + Uri.EscapeDataString(oauthversion) + """")
webRequest__1.Headers.Add("Authorization", authorizationHeaderParams.ToString)
webRequest__1.Method = "GET"
webRequest__1.ContentType = "application/x-www-form-urlencoded"
webRequest__1.Timeout = 3 * 60 * 1000
Try
Dim webResponse As HttpWebResponse = TryCast(webRequest__1.GetResponse(), HttpWebResponse)
Dim dataStream As System.IO.Stream = webResponse.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String
responseFromServer = reader.ReadToEnd()
Return responseFromServer
Catch ex As Exception
End Try
End Function
功能犯規代碼回報什麼。在包含調試模式下的reader.ReadToEnd()
的行中顯示「無法評估表達式」。
相同的代碼工作正常,如果該URL的格式如下 https://api.twitter.com/1.1/application/rate_limit_status.json 或 https://api.twitter.com/1.1/search/tweets.json?q=example
有什麼理由不爲流API的工作嗎?我不能使用搜索api,因爲它不會返回我需要的所有結果,例如包含url的tweets不會被搜索api返回。
我曾嘗試tweetinvi庫爲.net項目所建議的Twitter網站。
Public Sub Twit()
Dim oauthconsumerkey As String = "consumerkey"
Dim oauthconsumersecret As String = "consumersecret"
Dim oauthsignaturemethod As String = "HMAC-SHA1"
Dim oauthversion As String = "1.0"
Dim oauthtoken As String = "token"
Dim oauthtokensecret As String = "tokensecret"
Auth.SetUserCredentials(oauthconsumerkey, oauthconsumersecret, oauthtoken, oauthtokensecret)
Auth.ApplicationCredentials = New TwitterCredentials(oauthconsumerkey, oauthconsumersecret, oauthtoken, oauthtokensecret)
Dim filteredStream = Tweetinvi.Stream.CreateFilteredStream()
filteredStream.AddTrack("example")
Dim txt As String = ""
AddHandler filteredStream.MatchingTweetReceived, Sub(sender As Object, args As Tweetinvi.Core.Events.EventArguments.MatchedTweetReceivedEventArgs)
Console.WriteLine(args.Tweet.Text)
txt &= args.Tweet.Text
End Sub
filteredStream.StartStreamMatchingAllConditions()
End Sub
但是這並沒有返回任何東西。事件沒有被調用,我沒有得到任何結果。
這兩種方法都失敗了。是否還有其他可用於從vb.net網頁調用狀態/過濾器的明確示例?
使用Twetinvi沒有得到任何異常,代碼只是沒有做任何事情。我沒有添加異步功能,但仍然是一樣的。該功能在網頁上的page_load上被調用。你能否提供一個編碼示例如何獲取strean推文以檢查和循環它們? – Mahag
我會看看,我知道我有和例如的地方。 – Linvi