public override List<Result> Search(string pKeyWords) { string twitterConsumerKey = ConfigurationManager.AppSettings["twitter-consumer-key"]; string twitterConsumerSecret = ConfigurationManager.AppSettings["twitter-consumer-secret"]; if (string.IsNullOrWhiteSpace(twitterConsumerKey) || string.IsNullOrWhiteSpace(twitterConsumerSecret)) throw new Exception("App was unable to find Twitter credentials on the current settings file. Please add twitter-consumer-key and twitter-consumer-secret to the appSettings section."); ApplicationOnlyAuthorizer authorization = new ApplicationOnlyAuthorizer() { Credentials = new InMemoryCredentials() { ConsumerKey = twitterConsumerKey, ConsumerSecret = twitterConsumerSecret } }; authorization.Authorize(); if(!authorization.IsAuthorized) throw new Exception("Twitter authorizaton was unsuccessful. Please review your Twitter key and secret."); TwitterContext twitterContext = new TwitterContext(authorization); LinqToTwitter.Search twitterSearch = (from search in twitterContext.Search where search.Type == SearchType.Search && search.Query == pKeyWords && search.Count == this.MaxResultSearch select search) .SingleOrDefault(); IEnumerable<Status> tweets = from status in twitterSearch.Statuses orderby status.CreatedAt descending select status; List<Result> domainResults = new List<Result>(); foreach (Status status in tweets) { domainResults.Add( new Result() { CreatedDate = status.CreatedAt, Type = SourceType.Twitter, Text = status.Text, Title = status.Text.Length > 50? string.Format("{0}...",status.Text.Substring(0, 47)) : status.Text, URL = string.Format("http://twitter.com/{0}", status.User.Identifier.ScreenName) } ); } return domainResults; }
private static ITwitterAuthorizer DoApplicationOnly() { var auth = new ApplicationOnlyAuthorizer() { Credentials = new InMemoryCredentials { ConsumerKey = ConfigurationManager.AppSettings["consumerKey"], ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"] }, }; auth.Authorize(); return auth; }
protected void Page_Load(object sender, EventArgs e) { // Initialize object for paging pds = new PagedDataSource(); pds.AllowPaging = true; pds.PageSize = 5; // In v1.1, all API calls require authentication // Authentication auth = new ApplicationOnlyAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"], // Custom Custom Consumer Key ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"] // Custom Consumer Secret } }; // Authorize auth.Authorize(); // Instantiate a LinqToTwitter object twitterCtx = new TwitterContext(auth); // On first load if(! Page.IsPostBack){ // Initialize viewstate variables this.ViewState["CurrentPageIndex"] = 0; this.ViewState["PageCount"] = 0; // Invoke twitter databind TwitterDataBind(); // Set timer interval Timer1.Interval = REFRESHRATE * 1000; } // end if IsPostBack }
/// <summary> /// Demonstrates how to use ApplicationOnlyAuthorizer /// </summary> /// <param name="twitterCtx"></param> private static void HandleApplicationOnlyAuthentication() { var auth = new ApplicationOnlyAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"], ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"] } }; auth.Authorize(); //auth.Invalidate(); var twitterCtx = new TwitterContext(auth); var srch = (from search in twitterCtx.Search where search.Type == SearchType.Search && search.Query == "LINQ to Twitter" select search) .SingleOrDefault(); Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query); srch.Statuses.ForEach(entry => Console.WriteLine( "ID: {0, -15}, Source: {1}\nContent: {2}\n", entry.StatusID, entry.Source, entry.Text)); }
protected override void render(Graphics g) { if (W != 1059 || H != 94) Global.error("Drawing WidgetTwitter in unknown frame size: " + W + "x" + H); g.DrawImage(new Bitmap(Global.appData("1059x94 twitter.png")), new Rectangle(0, 0, W, H)); if (handles == null) return; var auth = new ApplicationOnlyAuthorizer(); auth.Credentials = new InMemoryCredentials { // @weatherfnord/multitweeterer ConsumerKey = "vTTLuEldRiVwqdBv97q6Q", ConsumerSecret = "4pLpDAjDAqovaY4DzQW7wj8ow9MeMmJ10nLlVED5zk" }; auth.Authorize(); var ctx = new TwitterContext(auth); var res = from search in ctx.Search where search.Type == SearchType.Search && search.Query == "LINQ to Twitter" select search; Search srch = res.Single(); IEnumerable<Status> tweets = Enumerable.Empty<Status>(); foreach (string h in handles.Split()) { string handle = h; if (h[0] == '@') handle = handle.Substring(1); try { var response = (from tweet in ctx.Status where tweet.Type == StatusType.User && tweet.ScreenName == handle && tweet.Count == 5 select tweet) .ToList(); tweets = tweets.Concat(response); } catch { } } tweets = tweets.OrderByDescending(item => item.CreatedAt); string text = ""; foreach (Status tweet in tweets) { text = tweet.ScreenName + ": " + tweet.Text.Replace("\n", " "); break; } fitText(g, Color.Black, "Calibri", text, new RectangleF(100, 16, 945, 54)); }