2015-11-12 57 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工作流程。