简体   繁体   English

使用Apache HttpClient和Oauth的路标连接到带有空格的轨道的Twitter流API?

[英]Connecting to Twitter streaming API with tracks with spaces using Apache HttpClient and signpost for Oauth?

Below is some code that tries to connect to Twitter using tracks with a space ("you will"). 下面是一些代码,这些代码尝试使用带有空格的轨道(“您会”)连接到Twitter。 It uses Apache HttpClient and signpost oauth in more or less the manner tweetstream4j does. 它或多或少使用tweetstream4j的方式使用Apache HttpClientopost 路标 I use 我用

  • signpost-core-1.2.1.1.jar 路标 - 核心1.2.1.1.jar
  • signpost-commonshttp4-1.2.1.1.jar 路标,commonshttp4-1.2.1.1.jar
  • httpclient-4.0.1.jar HttpClient的-4.0.1.jar
  • httpcore-4.0.1.jar 的HttpCore-4.0.1.jar

If I run the code, I get "401 unauthorized" from Twitter. 如果运行代码,则Twitter会显示“ 401未经授权”。 If use tracks without spaces (eg, "will" instead of "you will"), it connects and works fine (as long as I fill in oauth consumer key and secret, token, and token secret with valid values). 如果使用没有空格的轨道(例如,“将”而不是“您将”),它会连接并正常工作(只要我用有效值填写oauth使用者密钥和机密,令牌和令牌机密)。

Does anyone spot the problem? 有人发现问题了吗? I think it has to do with the URL encoding of the tracks parameter in the POST body, but I'm not sure. 我认为这与POST正文中tracks参数的URL编码有关,但我不确定。 I've been digging into how signpost works until my eyes bleed. 我一直在研究路标的工作原理,直到眼睛流血为止。

(This is related to Twitter streaming API in Java with tracks that have spaces? ). (这与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());
}
}

您应该将api链接中的http更改为https

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM