【问题标题】:Fatal error code 406 using hbc when filtering by location按位置过滤时使用 hbc 的致命错误代码 406
【发布时间】:2013-11-15 11:04:00
【问题描述】:

我正在尝试使用Twitter hbc library 来获取基于位置的推文。我已经根据示例文件FilterStreamExample 设置了我的代码。该示例有效,但是当我尝试向端点添加位置时,我收到致命错误代码:406。

这是有效的代码:

StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));

但是当我添加以下代码时,我得到了错误:

Coordinate southwest = new Coordinate(-87.648926, 41.883109);
Coordinate northeast = new Coordinate(-87.606354, 41.828050);
Location chicago = new Location(southwest, northeast); 
endpoint.locations(Lists.newArrayList(chicago));

感谢您的帮助!

格雷格

编辑 这是我的全部代码:

public class App {

  public static void oauth(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
    BlockingQueue<String> queue = new LinkedBlockingQueue<String>(100000);

    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
    Coordinate southwest = new Coordinate(-87.857666, 41.657347);
    Coordinate northeast = new Coordinate(-87.587128, 41.795712);
    Location chicago = new Location(southwest, northeast); 
    endpoint.locations(Lists.newArrayList(chicago));

    Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);

    BasicClient client = new ClientBuilder()
      .name("exampleStream")
      .hosts(Constants.STREAM_HOST)
      .endpoint(endpoint)
      .authentication(auth)
      .processor(new StringDelimitedProcessor(queue))
      .build();

    // Establish a connection
    client.connect();
    PrintWriter writer;
    try {
      writer = new PrintWriter("twitterstream.txt", "UTF-8"); 
// Do whatever needs to be done with messages
      for (int msgRead = 0; msgRead < 1000; msgRead++) {
        if (client.isDone()) {
          System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
          break;
      }
      String msg = queue.poll(5, TimeUnit.SECONDS);
      if (msg == null) {
        System.out.println("Did not receive a message in 5 seconds");
      } else {
        writer.println(msg);
      }
    }
     writer.close();
        } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
        }  

        client.stop();
}

  public static void main(String[] args) {
  try {
      App.oauth("LEDrJPDhn6C2trbde95WZg", "gEmlgewUodOoDjMNl2QbcqsbGvYDxNZWNRULchPE", "292576546-SWzEgMU7mC7Haat3lLEOQ0mXsZFwBf8F3ZKLBMVa", "ajPN2rGLx57yPoUjYe2qN4bE763orux4KwYeoCAvuLRPM");
  } catch (InterruptedException e) {
      System.out.println(e);
  }

}

【问题讨论】:

  • 我运行了你的代码,推文已写入文件,我没有看到任何错误。错误是否立即发生?您能否将错误/堆栈跟踪的详细信息添加到问题中?顺便说一句,您现在应该撤销您的 API 令牌,因为它们是公开可用的。
  • hm.. 看起来可能是凭据问题(尽管它适用于第一个查询而不是第二个查询很奇怪)......不过,谢谢您的帮助!

标签: java twitter twitter-streaming-api twitter-hbc


【解决方案1】:

我已经用你的和我的 Outh 参数检查了你的代码。对我的它有效,对你的无效。这就是问题

【讨论】:

  • 奇怪的是我可以使用相同的凭据发出其他请求,但
【解决方案2】:

您使用的是谷歌地图坐标吗? 因为看起来你应该切换你的坐标点。

Coordinate southwest = new Coordinate(41.657347, -87.857666);
Coordinate northeast = new Coordinate(41.795712, -87.587128);

您也可以使用 hbc 快照版本https://github.com/twitter/hbc/issues/19 问候。

【讨论】:

    【解决方案3】:

    也许问题出在坐标上。您应该在http://boundingbox.klokantech.com/ 上进行验证。之后选择“csv”选项来分隔位置。

    【讨论】:

    • 你是对的,我验证了我试图使用这个网站查找的点,并发现我以错误的顺序使用它们。
    猜你喜欢
    • 2017-09-13
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多