简单(Twitter + Streaming API + Java + OAuth)示例

时间:2012-12-17 02:57:00

标签: java api twitter oauth streaming

为了创建一个简单的Java程序来从Twitter的流API中提取推文,我修改了这个(http://cotdp.com/dl/TwitterConsumer.java)代码片段以使用OAuth方法。结果是下面的代码,在执行时抛出Connection Refused Exception。

我知道Twitter4J但我想创建一个至少依赖其他API的程序。

我完成了我的研究,看起来oauth.signpost库适合Twitter的流API。我还确保我的身份验证详细信息正确无误。我的Twitter访问级别是“只读”。

感谢任何指导。如果以前已经回答过这种类型的问题,我很抱歉,但是我找不到一个简单的Java示例来说明如何使用流API而不依赖于例如Twitter4j。​​

此致

AHL

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

/**
 * A hacky little class illustrating how to receive and store Twitter streams
 * for later analysis, requires Apache Commons HTTP Client 4+. Stores the data
 * in 64MB long JSON files.
 * 
 * Usage:
 * 
 * TwitterConsumer t = new TwitterConsumer("username", "password",
 *      "http://stream.twitter.com/1/statuses/sample.json", "sample");
 * t.start();
 */
public class TwitterConsumer extends Thread {
    //
    static String STORAGE_DIR = "/tmp";
    static long BYTES_PER_FILE = 64 * 1024 * 1024;
    //
    public long Messages = 0;
    public long Bytes = 0;
    public long Timestamp = 0;

    private String accessToken = "";
    private String accessSecret = "";
    private String consumerKey = "";
    private String consumerSecret = ""; 

    private String feedUrl;
    private String filePrefix;
    boolean isRunning = true;
    File file = null;
    FileWriter fw = null;
    long bytesWritten = 0;

    public static void main(String[] args) {
        TwitterConsumer t = new TwitterConsumer(
            "XXX", 
            "XXX",
            "XXX",
            "XXX",
            "http://stream.twitter.com/1/statuses/sample.json", "sample");
        t.start();
    }

    public TwitterConsumer(String accessToken, String accessSecret, String consumerKey, String consumerSecret, String url, String prefix) {
        this.accessToken = accessToken;
        this.accessSecret = accessSecret;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        feedUrl = url;
        filePrefix = prefix;
        Timestamp = System.currentTimeMillis();
    }

    /**
     * @throws IOException
     */
    private void rotateFile() throws IOException {
        // Handle the existing file
        if (fw != null)
            fw.close();
        // Create the next file
        file = new File(STORAGE_DIR, filePrefix + "-"
                + System.currentTimeMillis() + ".json");
        bytesWritten = 0;
        fw = new FileWriter(file);
        System.out.println("Writing to " + file.getAbsolutePath());
    }

    /**
     * @see java.lang.Thread#run()
     */
    public void run() {
        // Open the initial file
        try { rotateFile(); } catch (IOException e) { e.printStackTrace(); return; }
        // Run loop
        while (isRunning) {
            try {

                OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
                consumer.setTokenWithSecret(accessToken, accessSecret);
                HttpGet request = new HttpGet(feedUrl);
                consumer.sign(request);

                DefaultHttpClient client = new DefaultHttpClient();
                HttpResponse response = client.execute(request);
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent()));
                while (true) {
                    String line = reader.readLine();
                    if (line == null)
                        break;
                    if (line.length() > 0) {
                        if (bytesWritten + line.length() + 1 > BYTES_PER_FILE)
                            rotateFile();
                        fw.write(line + "\n");
                        bytesWritten += line.length() + 1;
                        Messages++;
                        Bytes += line.length() + 1;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Sleeping before reconnect...");
            try { Thread.sleep(15000); } catch (Exception e) { }
        }
    }
}
}

import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import oauth.signpost.OAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; /** * A hacky little class illustrating how to receive and store Twitter streams * for later analysis, requires Apache Commons HTTP Client 4+. Stores the data * in 64MB long JSON files. * * Usage: * * TwitterConsumer t = new TwitterConsumer("username", "password", * "http://stream.twitter.com/1/statuses/sample.json", "sample"); * t.start(); */ public class TwitterConsumer extends Thread { // static String STORAGE_DIR = "/tmp"; static long BYTES_PER_FILE = 64 * 1024 * 1024; // public long Messages = 0; public long Bytes = 0; public long Timestamp = 0; private String accessToken = ""; private String accessSecret = ""; private String consumerKey = ""; private String consumerSecret = ""; private String feedUrl; private String filePrefix; boolean isRunning = true; File file = null; FileWriter fw = null; long bytesWritten = 0; public static void main(String[] args) { TwitterConsumer t = new TwitterConsumer( "XXX", "XXX", "XXX", "XXX", "http://stream.twitter.com/1/statuses/sample.json", "sample"); t.start(); } public TwitterConsumer(String accessToken, String accessSecret, String consumerKey, String consumerSecret, String url, String prefix) { this.accessToken = accessToken; this.accessSecret = accessSecret; this.consumerKey = consumerKey; this.consumerSecret = consumerSecret; feedUrl = url; filePrefix = prefix; Timestamp = System.currentTimeMillis(); } /** * @throws IOException */ private void rotateFile() throws IOException { // Handle the existing file if (fw != null) fw.close(); // Create the next file file = new File(STORAGE_DIR, filePrefix + "-" + System.currentTimeMillis() + ".json"); bytesWritten = 0; fw = new FileWriter(file); System.out.println("Writing to " + file.getAbsolutePath()); } /** * @see java.lang.Thread#run() */ public void run() { // Open the initial file try { rotateFile(); } catch (IOException e) { e.printStackTrace(); return; } // Run loop while (isRunning) { try { OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); consumer.setTokenWithSecret(accessToken, accessSecret); HttpGet request = new HttpGet(feedUrl); consumer.sign(request); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(request); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); while (true) { String line = reader.readLine(); if (line == null) break; if (line.length() > 0) { if (bytesWritten + line.length() + 1 > BYTES_PER_FILE) rotateFile(); fw.write(line + "\n"); bytesWritten += line.length() + 1; Messages++; Bytes += line.length() + 1; } } } catch (Exception e) { e.printStackTrace(); } System.out.println("Sleeping before reconnect..."); try { Thread.sleep(15000); } catch (Exception e) { } } } } }

1 个答案:

答案 0 :(得分:1)

我试图模拟代码,发现错误非常简单。您应该在网址中使用https而不是http:)