如何在 MVC 项目中使用 linqtotwitter 搜索推文
How to search tweets using linqtotwitter on an MVC project
如何在 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));
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
});
}
}
}
我在其中手动编码了您的搜索查询,但看看它是如何使用 MvcAuthorizer
的。 SessionStateCredentialStore
从会话状态中提取先前获得的凭据。
如果您不喜欢会话状态,请在您希望保留凭据的位置实施 ICredentialStore
。
您还可以找到工作代码 MVC demo on the LINQ to Twitter GitHub site。
如何在 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));
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
});
}
}
}
我在其中手动编码了您的搜索查询,但看看它是如何使用 MvcAuthorizer
的。 SessionStateCredentialStore
从会话状态中提取先前获得的凭据。
如果您不喜欢会话状态,请在您希望保留凭据的位置实施 ICredentialStore
。
您还可以找到工作代码 MVC demo on the LINQ to Twitter GitHub site。