在创建与远程服务的连接时,Twitter Streaming Api错误

时间:2016-03-21 18:49:15

标签: java maven twitter-oauth scribe twitter-streaming-api

代码

   public abstract class TwitterStreamConsumerr {

      //  private static final String PROTECTED_RESOURCE_URL ="https://api.twitter.com/1.1/account/verify_credentials.json";

    private static final String PROTECTED_RESOURCE_URL = "https://stream.twitter.com/1.1/statuses/filter.json";

public static void main(String... args) throws IOException {
    final OAuth10aService service = new ServiceBuilder()
            .apiKey("xxxxxxxxxxxxxxxx")
            .apiSecret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
            .build(TwitterApi.instance());
    final Scanner in = new Scanner(System.in);

    System.out.println("=== Twitter's OAuth Workflow ===");
    System.out.println();

    // Obtain the Request Token
    System.out.println("Fetching the Request Token...");
    final OAuth1RequestToken requestToken = service.getRequestToken();
    System.out.println("Got the Request Token!");
    System.out.println();

    System.out.println("Now go and authorize ScribeJava here:");
    System.out.println(service.getAuthorizationUrl(requestToken));
    System.out.println("And paste the verifier here");
    System.out.print(">>");
    final String oauthVerifier = in.nextLine();
    System.out.println();

    // Trade the Request Token and Verfier for the Access Token
    System.out.println("Trading the Request Token for an Access Token...");
    final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
    System.out.println("Got the Access Token!");
    System.out.println("(if your curious it looks like this: " + accessToken
            + ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
    System.out.println();

    // Now let's go and ask for a protected resource!
    System.out.println("Now we're going to access a protected resource...");
    final OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL, service);
    service.signRequest(accessToken, request);
    final Response response = request.send();
    System.out.println("Got it! Lets see what we found...");
    System.out.println();
    System.out.println(response.getBody());

    System.out.println();
    System.out.println("That's it man! Go and build something awesome with ScribeJava! :)");

       request.addHeader("host", "stream.twitter.com");
        request.setConnectionKeepAlive(true);
        request.addHeader("user-agent", "Twitter Stream Reader");
        request.addBodyParameter("track", "java,heroku,twitter"); // Set keywords you'd like to track here
        service.signRequest(accessToken, request);
        Response response1 = request.send();

        // Create a reader to read Twitter's stream
        BufferedReader reader1 = new BufferedReader(new InputStreamReader(response.getStream()));

        String line;
        while ((line = reader1.readLine()) != null) {
            System.out.println(line);
        }
    }

}

错误

构建TwitterStreamConsumer 1.0-SNAPSHOT

--- exec-maven-plugin:1.2.1:exec(default-cli)@TwitterStreamConsumer --- === Twitter的OAuth工作流程===

获取请求令牌... 得到了请求令牌!

现在去这里授权ScribeJava: https://api.twitter.com/oauth/authorize?oauth_token=S4nXCQAAAAAAji5sAAABU5p16Cg 并在此处粘贴验证程序

  
    

2440695

  

交易请求令牌以获取访问令牌... 获得了访问令牌!

现在我们要访问受保护资源了... 得到它了!让我们看看我们发现了什么...

未找到过滤器参数。预计至少有一个参数:跟踪轨道位置

那是男人!使用ScribeJava去构建一些非常棒的东西! :) 线程" main"中的例外情况*** com.github.scribejava.core.exceptions.OAuthConnectionException:创建与远程服务的连接时出现问题:https://stream.twitter.com/1.1/statuses/filter.json     在com.github.scribejava.core.model.OAuthRequest.send(OAuthRequest.java:39)     在com.mycompany.twitterstreamconsumer.TwitterStreamConsumerr.main(TwitterStreamConsumerr.java:72) 引起:java.lang.IllegalStateException:正在进行连接     at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:517)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:374)     在com.github.scribejava.core.model.OAuthRequest.doSend(OAuthRequest.java:45)     在com.github.scribejava.core.model.OAuthRequest.send(OAuthRequest.java:37)

... 1更多***

建筑失败

总时间:33.684秒 完成于:2016年3月21日23:35:59 PKT 2016

最终记忆:4M / 60M

无法在项目TwitterStreamConsumer上执行目标org.codehaus.mojo:exec-maven-plugin:1.2.1:exec(default-cli):命令执行失败。退出并出现错误:1(退出值:1) - > [帮助1]

要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。 使用-X开关重新运行Maven以启用完整的调试日志记录。

有关错误和可能的解决方案的更多信息,请阅读以下文章: [帮助1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

问题

我试图通过Twitter流式传输数据Api.Scribe java用于身份验证,正如您在输出中看到的那样运行良好,但完全不知道为什么现在它给出了错误。感谢任何帮助

0 个答案:

没有答案