Twitter Streaming API の仕様変更

先日,Twitter社から "Streaming APISSL でのアクセスに変更する"旨のアナウンスがなされた。

Streaming API turning SSL only on September 29th
https://dev.twitter.com/blog/streaming-api-turning-ssl-only-september-29th

要は http を https へ書き換えれば動く訳で,Basic 認証ならば下記のプログラムでOK。

import java.net.*;
import java.io.*;
 
public class TwitterStreamingBasicAuth {
  public static void main(String[] args) {
    try {
      // BASIC認証の設定
      final String username = "Twitterユーザ名";
      final String password = "Twitterパスワード";
      Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(username, password.toCharArray());
        }
      });

      // HTTPリクエスト
      URL url = new URL("https://stream.twitter.com/1/statuses/sample.json");
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 
      // レスポンスコード
      System.err.printf("%d %s\n", connection.getResponseCode(), connection.getResponseMessage());
 
      // 成功ならレスポンスボディを表示する
      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),
                "UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
	    System.out.println(line);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

ただし,仕様変更のページには

On a related note, Basic Auth will eventually be deprecated on the Streaming API, and we recommend that you switch to OAuth well in advance.

とも書かれているため,OAuth へ移行する必要もある。
これまで OAuth は Twitter4J に頼ってきたから,山本祐介氏の対応に期待するか,自分でベタに書くかを選択しなければならない。と書きつつ,ふとTwitter を眺めたら "t4j_news new #twitter4j 2.2.5-SNAPSHOT is out. http://t.co/q2cp1bg1 fixes TFJ-632. http://t.co/EwI18LRk thanks" というメッセージが流れている。対応が素早い!