Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq to twitter LinqToTwitter搜索永远不会返回_Linq To Twitter - Fatal编程技术网

Linq to twitter LinqToTwitter搜索永远不会返回

Linq to twitter LinqToTwitter搜索永远不会返回,linq-to-twitter,Linq To Twitter,我正在尝试使用LinqToTwitter搜索twitter。它可以在NUnit测试中运行,但不能与ASP.NET或WinForm应用程序一起运行。我不确定使用什么授权人 public async Task<Search> SearchTwitter(string searchWords) { var twitterCtx = BuildTwitterContext(); Task<Search> searchResponse = (from search

我正在尝试使用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;
}
public异步任务SearchTwitter(字符串searchWords)
{
var twitterCtx=BuildTwitterContext();
TaskSearchResponse=(来自twitterCtx.search中的搜索
其中search.Type==SearchType.search&&
search.Query==searchWords
选择(搜索)
.SingleOrDefaultAsync();
返回等待搜索响应;
}
私有静态TwitterContext BuildTwitterContext()
{
授权人授权人;
if(HttpContext.Current==null)
授权人=新的授权人();
其他的
authorizer=新的AspNetSignInAuthorizer();
InMemoryCredentialStore credentialStore=新的InMemoryCredentialStore();
credentialStore.ConsumerKey=消费市场;
credentialStore.ConsumerCret=ConsumerCret;
credentialStore.OAuthToken=accessToken;
credentialStore.OAuthTokenSecret=accessTokenSecret;
authorizer.CredentialStore=凭证存储;
var twitterCtx=新TwitterContext(授权人);
返回twitterCtx;
}

ASP.NET的不同之处在于页面重定向,即启动授权,然后在Twitter重定向回来后完成。以下是LINQ to Twitter文档,它将解释OAuth是如何工作的,并让您更好地了解使用哪些授权人:

L2T源代码也有演示。下面是OAuth控制器演示:

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


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

ASP.NET的不同之处在于页面重定向,即启动授权,然后在Twitter重定向回来后完成。以下是LINQ to Twitter文档,它将解释OAuth是如何工作的,并让您更好地了解使用哪些授权人:

L2T源代码也有演示。下面是OAuth控制器演示:

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

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

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