【问题标题】:How to search tweets using linqtotwitter on an MVC project如何在 MVC 项目中使用 linqtotwitter 搜索推文
【发布时间】:2018-02-10 01:33:53
【问题描述】:

如何在 MVC 项目中使用 linqtotwitter 搜索推文,并将其从控制台转换为 MVC。

var searchResponse =
            await
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == "\"LINQ to Twitter\""
             select search)
            .SingleOrDefaultAsync();

        if (searchResponse != null && searchResponse.Statuses != null)
            searchResponse.Statuses.ForEach(tweet =>
                Console.WriteLine(
                    "User: {0}, Tweet: {1}", 
                    tweet.User.ScreenNameResponse,
                    tweet.Text));

【问题讨论】:

    标签: c# asp.net twitter linq-to-twitter


    【解决方案1】:

    MVC 的主要作用是使用正确的授权者。我使用的技术是一个单独的控制器来处理 OAuth,如下所示:

    using System;
    using System.Configuration;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    using LinqToTwitter;
    
    namespace MvcDemo.Controllers
    {
        public class OAuthController : AsyncController
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public async Task<ActionResult> BeginAsync()
            {
                //var auth = new MvcSignInAuthorizer
                var auth = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore
                    {
                        ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                        ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
                    }
                };
    
                string twitterCallbackUrl = Request.Url.ToString().Replace("Begin", "Complete");
                return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl));
            }
    
            public async Task<ActionResult> CompleteAsync()
            {
                var auth = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore()
                };
    
                await auth.CompleteAuthorizeAsync(Request.Url);
    
                // This is how you access credentials after authorization.
                // The oauthToken and oauthTokenSecret do not expire.
                // You can use the userID to associate the credentials with the user.
                // You can save credentials any way you want - database, 
                //   isolated storage, etc. - it's up to you.
                // You can retrieve and load all 4 credentials on subsequent 
                //   queries to avoid the need to re-authorize.
                // When you've loaded all 4 credentials, LINQ to Twitter will let 
                //   you make queries without re-authorizing.
                //
                //var credentials = auth.CredentialStore;
                //string oauthToken = credentials.OAuthToken;
                //string oauthTokenSecret = credentials.OAuthTokenSecret;
                //string screenName = credentials.ScreenName;
                //ulong userID = credentials.UserID;
                //
    
                return RedirectToAction("Index", "Home");
            }
        }
    }
    

    请注意,这是一个 MvcAuthorizer 和一个 SessionStateCredentialStore。此外,请记住确保您的状态服务器正在运行,这样它就不会在 IIS 回收期间任意转储您的会话状态。所以现在,当您需要检测用户是否未经授权时,请将其发送到OAuthController,如下所示:

    using LinqToTwitter;
    using System.Web.Mvc;
    
    namespace MvcDemo.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                if (!new SessionStateCredentialStore().HasAllCredentials())
                    return RedirectToAction("Index", "OAuth");
    
                return View();
            }
        }
    }
    

    一旦用户获得授权,您就可以在操作方法中执行任何 LINQ to Twitter 查询,如下所示:

    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    using MvcDemo.Models;
    using LinqToTwitter;
    using System.Collections.Generic;
    
    namespace MvcDemo.Controllers
    {
        public class StatusDemosController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult Tweet()
            {
                var sendTweetVM = new SendTweetViewModel
                {
                    Text = "Testing async LINQ to Twitter in MVC - " + DateTime.Now.ToString()
                };
    
                return View(sendTweetVM);
            }
    
            [HttpPost]
            [ActionName("Tweet")]
            public async Task<ActionResult> TweetAsync(SendTweetViewModel tweet)
            {
                var auth = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore()
                };
    
                var ctx = new TwitterContext(auth);
    
                Status responseTweet = await ctx.TweetAsync(tweet.Text);
    
                var responseTweetVM = new SendTweetViewModel
                {
                    Text = "Testing async LINQ to Twitter in MVC - " + DateTime.Now.ToString(),
                    Response = "Tweet successful! Response from Twitter: " + responseTweet.Text
                };
    
                return View(responseTweetVM);
            }
    
            [ActionName("HomeTimeline")]
            public async Task<ActionResult> HomeTimelineAsync()
            {
                var auth = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore()
                };
    
                var ctx = new TwitterContext(auth);
    
                var tweets =
                    await
                    (from tweet in ctx.Status
                     where tweet.Type == StatusType.Home
                     select new TweetViewModel
                     {
                         ImageUrl = tweet.User.ProfileImageUrl,
                         ScreenName = tweet.User.ScreenNameResponse,
                         Text = tweet.Text
                     })
                    .ToListAsync();
    
                return View(tweets);
            }
    
            [ActionName("Search")]
            public async Task<ActionResult> SearchAsync()
            {
                var auth = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore()
                };
    
                var ctx = new TwitterContext(auth);
    
                var searchResponse =
                    await
                    (from search in twitterCtx.Search
                     where search.Type == SearchType.Search &&
                           search.Query == "\"LINQ to Twitter\""
                     select search)
                    .SingleOrDefaultAsync();
    
                return View(searchResponse.Statuses);
            }
    
            [ActionName("UploadImage")]
            public async Task<ActionResult> UploadImageAsync()
            {
                var auth = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore()
                };
    
                var twitterCtx = new TwitterContext(auth);
    
                string status = $"Testing multi-image tweet #Linq2Twitter £ {DateTime.Now}";
                string mediaCategory = "tweet_image";
    
                string path = Server.MapPath("..\\Content\\200xColor_2.png");
                var imageUploadTasks =
                    new List<Task<Media>>
                    {
                        twitterCtx.UploadMediaAsync(System.IO.File.ReadAllBytes(path), "image/jpg", mediaCategory),
                    };
    
                await Task.WhenAll(imageUploadTasks);
    
                List<ulong> mediaIds =
                    (from tsk in imageUploadTasks
                     select tsk.Result.MediaID)
                    .ToList();
    
                Status tweet = await twitterCtx.TweetAsync(status, mediaIds);
    
                return View(
                    new TweetViewModel
                    {
                        ImageUrl = tweet.User.ProfileImageUrl,
                        ScreenName = tweet.User.ScreenNameResponse,
                        Text = tweet.Text
                    });
            }
        }
    }
    

    我在那里手动编码了您的搜索查询,但看看它如何使用MvcAuthorizerSessionStateCredentialStore 从 Session 状态拉取之前获得的凭据。

    如果您不喜欢 Session 状态,请在您想要保存凭据的位置实现 ICredentialStore

    您还可以找到有效的MVC demo on the LINQ to Twitter GitHub site 的代码。

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多