2015-11-12 54 views
1

我正在尝试使用LinqToTwitter来搜索twitter。它在NUnit测试中运行良好,但不适用于ASP.NET或WinForm应用程序。我不确定要使用哪个授权者。LinqToTwitter搜索永不返回

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

回答

0

ASP.NET的不同之处在于页面重定向在哪里开始授权,然后在Twitter重定向之后完成。这里的LINQ到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工作流程。