LinqToTwitter搜索从不返回

时间:2015-11-12 19:54:34

标签: linq-to-twitter

我正在尝试使用LinqToTwitter来搜索twitter。它可以在NUnit测试中运行,但它不能用于ASP.NET或WinForm应用程序。我不确定使用什么授权人。

public async Task<Search> SearchTwitter(string searchWords)
{
    var twitterCtx = BuildTwitterContext();

    Task<Search> searchResponse = (from search in twitterCtx.Search
                                   where search.Type == SearchType.Search &&
                                         search.Query == searchWords
                                   select search)
        .SingleOrDefaultAsync();

    return await searchResponse;
}

private static TwitterContext BuildTwitterContext()
{
    IAuthorizer authorizer;

    if (HttpContext.Current == null)
        authorizer = new PinAuthorizer();
    else
        authorizer = new AspNetSignInAuthorizer();

    InMemoryCredentialStore credentialStore = new InMemoryCredentialStore();
    credentialStore.ConsumerKey = consumerKey;
    credentialStore.ConsumerSecret = consumerSecret;
    credentialStore.OAuthToken = accessToken;
    credentialStore.OAuthTokenSecret = accessTokenSecret;
    authorizer.CredentialStore = credentialStore;
    var twitterCtx = new TwitterContext(authorizer);
    return twitterCtx;
}

1 个答案:

答案 0 :(得分:0)

ASP.NET因页面重定向而不同,您可以在其中启动授权,然后在Twitter重定向后完成。这是LINQ to Twitter文档,它将解释OAuth的工作原理,并让您更好地了解使用哪些授权者:

https://github.com/JoeMayo/LinqToTwitter/wiki/Learning-to-use-OAuth

L2T源代码也有演示。这是一个OAuth控制器演示:

https://github.com/JoeMayo/LinqToTwitter/blob/master/New/Linq2TwitterDemos_Mvc/Controllers/OAuthController.cs

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");
    }
}

请注意,它使用WebAuthorizer / SessionStateCredentials对,并将授权的开始与单独的操作方法(通过回调指定)分开以完成。

以下演示演示了如何在WinForms应用程序中执行OAuth:

https://github.com/JoeMayo/LinqToTwitter/blob/master/New/Demos/Linq2TwitterDemos_WindowsForms/OAuthForm.cs

public partial class OAuthForm : Form
{
    PinAuthorizer pinAuth = new PinAuthorizer();

    public OAuthForm()
    {
        InitializeComponent();
    }

    async void OAuthForm_Load(object sender, EventArgs e)
    {
        pinAuth = new PinAuthorizer
        {
            // Get the ConsumerKey and ConsumerSecret for your app and load them here.
            CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
            },
            // Note: GetPin isn't used here because we've broken the authorization
            // process into two parts: begin and complete
            GoToTwitterAuthorization = pageLink => 
                OAuthWebBrowser.Navigate(new Uri(pageLink, UriKind.Absolute))
        };

        await pinAuth.BeginAuthorizeAsync();
    }

    async void SubmitPinButton_Click(object sender, EventArgs e)
    {
        await pinAuth.CompleteAuthorizeAsync(PinTextBox.Text);
        SharedState.Authorizer = pinAuth;

        // 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 = pinAuth.CredentialStore;
        //string oauthToken = credentials.OAuthToken;
        //string oauthTokenSecret = credentials.OAuthTokenSecret;
        //string screenName = credentials.ScreenName;
        //ulong userID = credentials.UserID;
        //

        Close();
    }
}

在这种情况下,您可以将PinAuthorizer与InMemoryCredentialStore一起使用。如果您查看该演示,它会使用Web浏览器控件导航到Twitter并管理OAuth流程。

请查看上面的URL,了解学习如何使用OAuth,以获取可在不同场景中使用的其他IAuthorizer派生类型的示例。此外,下载源代码并使用调试器逐步完成,以了解OAuth工作流程。