0

我一直在使用 Linq to Twitter 使用 OAuth 对 Twitter API 1.1 进行身份验证。我设置了我的凭据,然后将它们传递给 Linq to Twitter,这表明我 isAuth 返回 true。但是,在运行时我收到 215“错误的身份验证数据”错误。有没有其他人有这个问题?

  var auth = new SingleUserAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = TwitterSettings.ConsumerKey,
                ConsumerSecret = TwitterSettings.ConsumerKeySecret,
                OAuthToken = TwitterSettings.AccessToken,
                AccessToken = TwitterSettings.AccessTokenSecret,
            }
        };
        auth.Authorize();
    }

这是 if else 说明我已获得授权:

        if (auth == null || !auth.IsAuthorized)
        {

        }
        // If Twitter Authorizes Application 
        if (auth.IsAuthorized && i == 1)
        {

            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(new Uri("https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4"));
            string json = await response.Content.ReadAsStringAsync();

            JObject obj = JObject.Parse(json);
            var jsonResults = obj["results"];

            await JsonConvert.DeserializeObjectAsync<List<Tweet>>(jsonResults.ToString());

        }

我能够进入代码的 auth.IsAtuhorized 部分,但响应返回 215 错误。我也知道我的 URI 是正确的,因为我直接从 Twitter API 1.1 示例页面复制它来测试调用。提前致谢

4

1 回答 1

0

您正在正确使用授权方,但是您没有使用 LINQ to Twitter 进行查询。您正在使用 HttpClient ,并且这两个 API 之间没有关系。以下是如何使用 LINQ to Twitter 执行搜索的示例:

    var srch =
        (from search in twitterCtx.Search
         where search.Type == SearchType.Search &&
               search.Query == "LINQ to Twitter" &&
               search.Count == 7
         select search)
        .SingleOrDefault();

    Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query);
    srch.Statuses.ForEach(entry =>
        Console.WriteLine(
            "ID: {0, -15}, Source: {1}\nContent: {2}\n",
            entry.StatusID, entry.Source, entry.Text));

您可以访问LINQ to Twitter 搜索文档以获取有关可用参数的更多信息。

更新,这是一个异步示例:

    (from search in twitterCtx.Search
     where search.Type == SearchType.Search &&
           search.Query == QueryTextBox.Text
     select search)
    .MaterializedAsyncCallback(asyncResponse =>
        Dispatcher.BeginInvoke(() =>
        {
            if (asyncResponse.Status != TwitterErrorStatus.Success)
            {
                MessageBox.Show("Error during query: " + asyncResponse.Exception.Message);
                return;
            }

            Search search = asyncResponse.State.SingleOrDefault();
            var tweets =
                (from status in search.Statuses
                 select new Tweet
                 {
                     UserName = status.User.Identifier.ScreenName,
                     Message = status.Text,
                     ImageSource = status.User.ProfileImageUrl
                 })
                .ToList();

            SearchListBox.ItemsSource = tweets;
        }));
于 2013-05-08T05:27:39.220 回答