14434

Need help about Search and Streaming Api of Twitter

Question:

I'm a newbie in C# and asp.net. I'm trying to create a simple web(run on localhost) which use streaming and search API of Twitter.

I've tried some query like: "<a href="https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799" rel="nofollow">https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799</a>" but it got some error about "Bad Authentication data".

May be I don't know how to get Authentication. So could anyone help me? THanks a lots.

Answer1:

What you have shown is not the search functionality, rather is the stream functionality. Here is a complete example of working twitter stream search

private void SearchTweetsWithQuery(string query) { var oauth_token = System.Configuration.ConfigurationManager.AppSettings["accessToken"]; var oauth_token_secret = System.Configuration.ConfigurationManager.AppSettings["accessTokenSecret"]; var oauth_consumer_key = System.Configuration.ConfigurationManager.AppSettings["consumerKey"]; var oauth_consumer_secret = System.Configuration.ConfigurationManager.AppSettings["consumerSecret"]; // oauth implementation details var oauth_version = "1.0"; var oauth_signature_method = "HMAC-SHA1"; // unique request details var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(); // message api details var status = "Updating status via REST API if this works"; var resource_url = "https://api.twitter.com/1.1/search/tweets.json"; var screen_name = "TODO : UPDATE THIS OR LEAVE BLANK"; var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}"; var baseString = string.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version, query ); baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString)); var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", Uri.EscapeDataString(oauth_token_secret)); string oauth_signature; using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))) { oauth_signature = Convert.ToBase64String( hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString))); } // create the request header var headerFormat = "OAuth oauth_consumer_key=\"{3}\", oauth_nonce=\"{0}\", oauth_signature=\"{5}\", oauth_signature_method=\"{1}\", " + "oauth_timestamp=\"{2}\", " + "oauth_token=\"{4}\", " + "oauth_version=\"{6}\""; var authHeader = 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) ); // make the request ServicePointManager.Expect100Continue = false; var postBody = "screen_name=" + Uri.EscapeDataString(screen_name); resource_url += "?q=" + query; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url); request.Headers.Add("Authorization", authHeader); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; WebResponse response = request.GetResponse(); string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); }

Answer2:

First you should create an application. <a href="https://dev.twitter.com/apps" rel="nofollow">https://dev.twitter.com/apps</a> . Then you can use Twitterizer's WebRequestBuilder class to form your request. ( <a href="http://svn.twitterizer.net/twitterizer/trunk/Twitterizer2/OAuth/" rel="nofollow">http://svn.twitterizer.net/twitterizer/trunk/Twitterizer2/OAuth/</a> )

var tokens = new Twitterizer.OAuthTokens { ConsumerKey = "....", ConsumerSecret = "....", AccessToken = "....", AccessTokenSecret = "......" }; string url = "https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799"; var builder = new Twitterizer.WebRequestBuilder(new Uri(url), Twitterizer.HTTPVerb.GET, tokens); var req = builder.PrepareRequest(); var resp = req.GetResponse(); var json = new StreamReader(resp.GetResponseStream()).ReadToEnd(); resp.Close();

Recommend

  • Delphi XE2 - always getting 401 Unauthorized () trying to get OAuth Access Token from Intuit
  • Access user's iOS default Twitter account iOS
  • How do you retrieve a users' access token and secret in ASP.NET 5 Identity?
  • Getting Error 503 with UrlFetchApp.fetch
  • How to integrate WooComerceAPI into React?
  • Etsy Image add returning 500 error - metadata doesn't look like a _FILES array
  • Quickbook Intergration Using PHP
  • Using Twitter login API
  • TableViewController: Async call, numberOfRowsInSection returns 0
  • Convert curl command to pycurl
  • Access additional twitter user info
  • Twitter Request Token Wahts Wrong with this HTTP?
  • Generating Authentication Header
  • Twit to twitter using MGTwitterEngine not Working
  • oAuth and Codeigniter with MongoDB
  • How do I get online predictions in javascript for my model on Cloud Machine Learning Engine?
  • Keep getting 403 Forbidden from Google API (using PHP client library v1)
  • instagram api - cryptic response; not sure if working and unable to test omniauth with this
  • Meteor,Access Google contacts of loggedin user
  • JavaScript OAuth2 flow for Azure AD v2 login does not give an access_token
  • oAuth client in Office VBA what is difficult?
  • MGTwitterEngine for Iphone Authentication xAuth oAuth
  • Getting Request Token From Dropbox OAuth “Invalid OAuth Request”
  • Android: Balancing market presence, API function availability, and approach to cleint/server authent
  • twitter retweet user id api python
  • Social networking integration with OAuth in iPhone
  • flickr, tumblr , instagram share intent in android
  • withings api authentication
  • How to access Graph API from a Web API with ADAL JS issued implicit grant token
  • Laravel 5.3 : Passport Implementation - {“error”:“invalid_client”,“message”:“Client authentication f
  • How to disable printing request information while using twitter4j
  • Google latitude returns empty location resource. Anything wrong in this Oauth flow?
  • How to validate or verify JWT Signature?
  • Parse url hash fragment in express application
  • TypeError upon authenticating user using Google OAuth 2
  • Google Contacts API asp.net settings and authorization token
  • Unable to save a query as a view table
  • OAuth2 flow for mobile app
  • Circular dependency while pushing http interceptor