首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从vb.net网页调用推特流接口(状态/过滤器)

从vb.net网页调用推特流接口(状态/过滤器)
EN

Stack Overflow用户
提问于 2016-05-17 15:08:00
回答 1查看 310关注 0票数 0

我正在尝试调用我的.net网页中的流应用编程接口过滤器。

我确实在twitter上创建了一个应用程序,并获取了所需的凭据(消费者密钥、消费者秘密、访问令牌和访问令牌秘密)

这是我使用的代码

代码语言:javascript
复制
 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()的行上显示"Unable to evaluate expression“。

如果url的格式为https://api.twitter.com/1.1/application/rate_limit_status.jsonhttps://api.twitter.com/1.1/search/tweets.json?q=example,则相同的代码也可以正常工作

为什么它对流apis不起作用?我不能使用搜索api,因为它不会返回我需要的所有结果,例如,包含urls的tweet不会被搜索api返回。

我确实按照twitter网站的建议尝试了.net项目的tweetinvi库。

代码语言:javascript
复制
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

但这不会返回任何内容。事件没有被调用,我也没有得到任何结果。

这两种方法failed.Is有没有其他清晰的例子,我可以用来调用vb.net网页的状态/过滤器?

EN

回答 1

Stack Overflow用户

发布于 2016-05-17 17:55:22

我是Tweetinvi的开发者。

您的代码看起来是正确的。你能告诉我你收到了什么例外吗?

此外,我不能确定是否直接在页面中调用此代码(我不熟悉WebForms),但请不要担心StartStreamMatchingAllConditions将阻塞它在其中运行的Thread,直到流停止。

如果希望它在后台运行,则需要使用async版本:StartStreamMatchingAllConditionsAsync()

最后,我将流tweet发送到UI的方法是使用WebSocket,并将信息从服务器发送到javascript处理程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37269367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档