1
JavaでStreaming APIを使用しているときに問題が発生しました。多くのサイトを訪問しましたが、コードの問題を特定できませんでした。 リモートサービスとの接続ストリーミングAPI
import java.util.Scanner;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.apis.TwitterApi;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public abstract class TwitterStreamConsumerr {
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("xxxxxxxxxxxxxxxxxxxxxxxxxx")
.apiSecret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.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! :)");
OAuthRequest request1 = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL, service);
request1.addHeader("version", "HTTP/1.1");
request1.addHeader("host", "stream.twitter.com");
request1.setConnectionKeepAlive(true);
request1.addHeader("user-agent", "Twitter Stream Reader");
request1.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(response1.getStream()));
String line;
while ((line = reader1.readLine()) != null) {
System.out.println(line);
}
}
}
私はデバッガでこのプログラムを実行
が、私はその例外が発生whisコードは、以下に示す行の末尾に到達した:は、ここで私が使用しているコードです。
と例外です:
Exception in thread "main" com.github.scribejava.core.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service: https://stream.twitter.com/1.1/statuses/filter.json
at com.github.scribejava.core.model.OAuthRequest.send(OAuthRequest.java:39)
at com.mycompany.twitterstreamconsumer.TwitterStreamConsumerr.main(TwitterStreamConsumerr.java:81)
Caused by: java.lang.IllegalStateException: connect in progress
at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:517)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:374)
at com.github.scribejava.core.model.OAuthRequest.doSend(OAuthRequest.java:45)
at com.github.scribejava.core.model.OAuthRequest.send(OAuthRequest.java:37)
私はMavenを使用していて、私のpom.xmlは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>TwitterStreamConsumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-apis</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
</project>
誰かが私を見つけるのに役立つ場合、私は非常に感謝するでしょうこの問題。私はその問題はここにあると思い
こんにちは@のwonderb0ltの
Response response1 = request.send();
を使用して、ここで私は私を強化しようとしました質問。 –