以下は、スペース付きのトラック ("you will") を使用して Twitter に接続しようとするコードです。多かれ少なかれtweetstream4jと同じように、 Apache HttpClientとサインポスト oauthを使用します。私が使う
- 標識コア-1.2.1.1.jar
- signpost-commonshttp4-1.2.1.1.jar
- httpclient-4.0.1.jar
- httpcore-4.0.1.jar
コードを実行すると、Twitter から「401 無許可」が返されます。スペースなしでトラックを使用する場合 (たとえば、「you will」の代わりに「will」)、接続して正常に動作します (oauth コンシューマー キーとシークレット、トークン、およびトークン シークレットに有効な値を入力する限り)。
誰かが問題を見つけますか?POST本体のtracksパラメータのURLエンコーディングに関係していると思いますが、よくわかりません。目から血が出るまで、道しるべがどのように機能するかを掘り下げてきました。
(これはJava の Twitter ストリーミング API に関連しており、トラックにスペースが含まれていますか? )。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.logging.Logger;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class TwitterSpaceTest {
private static Logger logger = Logger.getLogger(
TwitterSpaceTest.class.getName());
private static final String API_PROTOCOL = "http";
private static final String API_DOMAIN = "stream.twitter.com";
private static final String API_URL = "1/";
private static final String FILTER_URL = API_URL + "statuses/filter.json";
public static void readFrom(HttpRequestBase requestMethod) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(requestMethod);
HttpEntity entity = response.getEntity();
if (entity == null) throw new IOException("No entity");
BufferedReader br = new BufferedReader(new InputStreamReader(
entity.getContent(), "UTF-8"));
while (true) {
String line = br.readLine();
if (line != null && !line.isEmpty()) {
System.out.println(line);
}
}
}
public static HttpRequestBase getConnection()
throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException,
IllegalStateException, IOException {
String base = FILTER_URL;
List<NameValuePair> params = new ArrayList<NameValuePair>();
// No spaces
// params.add(new BasicNameValuePair("track", "will"));
// Spaces
params.add(new BasicNameValuePair("track", "you will"));
HttpPost method = new HttpPost(API_PROTOCOL + "://" + API_DOMAIN + "/"
+ base);
logger.info("" + method.getURI());
UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(params,
HTTP.UTF_8);
method.setEntity(postEntity);
String consumerKey = "...";
String consumerSecret = "..."
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(
consumerKey, consumerSecret);
String token = "...";
String tokenSecret = "...";
consumer.setTokenWithSecret(token, tokenSecret);
consumer.sign(method);
return method;
}
public static void main(String[] args) throws Exception {
readFrom(getConnection());
}
}