我正在尝试在.net网页中调用Streaming API过滤器。
我确实在Twitter上创建了一个应用程序并获取了所需的凭据(消费者密钥,消费者密钥,访问令牌和访问令牌秘密)
这是使用
的代码void scr2obj(double &ox,double &oy,double sx,double sy)
{
// ox=(sx-x0)/zoom;
// oy=(sy-y0)/zoom;
ox=(sx/zoom)-x0;
oy=(sy/zoom)-y0;
}
//---------------------------------------------------------------------------
void obj2scr(double &sx,double &sy,double ox,double oy)
{
// sx=x0+(ox*zoom);
// sy=y0+(oy*zoom);
sx=(x0+ox)*zoom;
sy=(y0+oy)*zoom;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseWheelDown(TObject *Sender, TShiftState Shift,TPoint &MousePos, bool &Handled)
{
double mx0,my0;
scr2obj(mx0,my0,mx,my);
zoom/=1.25; // zoom out
obj2scr(mx0,my0,mx0,my0);
// x0+=mx-mx0;
// y0+=my-my0;
x0+=(mx-mx0)/zoom;
y0+=(my-my0)/zoom;
_redraw=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseWheelUp(TObject *Sender, TShiftState Shift, TPoint &MousePos, bool &Handled)
{
double mx0,my0;
scr2obj(mx0,my0,mx,my);
zoom*=1.25; // zoom in
obj2scr(mx0,my0,mx0,my0);
// x0+=mx-mx0;
// y0+=my-my0;
x0+=(mx-mx0)/zoom;
y0+=(my-my0)/zoom;
_redraw=true;
}
//---------------------------------------------------------------------------
该功能不会返回任何内容。在调试模式下包含 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
的行显示“无法计算表达式”。
如果网址采用此格式,则相同的代码可以正常工作 https://api.twitter.com/1.1/application/rate_limit_status.json 要么 https://api.twitter.com/1.1/search/tweets.json?q=example
为什么它不适用于流apis?我不能使用搜索API,因为它不会返回我需要的所有结果,例如搜索API不会返回包含网址的推文。
我按照Twitter网站的建议为.net项目尝试了tweetinvi库。
reader.ReadToEnd()
但这不会返回任何东西。事件没有被调用,我没有得到任何结果。
两种方法都失败了。还有其他明显的例子我可以用来从vb.net网页调用状态/过滤器吗?
答案 0 :(得分:0)
我是Tweetinvi的开发者。
您的代码看起来是正确的。你能不能告诉我你收到的例外情况。
另外,我不确定是否直接在页面中调用此代码(我不熟悉WebForms),但请注意StartStreamMatchingAllConditions
将阻止它运行的Thread
直到溪流停止。
如果您想让它在场景后面运行,您需要使用async
版本:StartStreamMatchingAllConditionsAsync()
。
最后,我将流推文发送到UI的方式是使用WebSocket并将信息从服务器发送到javascript处理程序。