我在Java中使用Streaming 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);
}
}
}
当我用调试器运行这个程序时,我发现异常发生,代码到达下面显示的行尾。
而且例外是:
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>
如果有人帮我找到这个问题,我将非常感激。
答案 0 :(得分:1)
我认为问题在这里,你创建了
OAuthRequest request1 = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL, service);
但在代码中使用
Response response1 = request.send();
代替
Response response1 = request1.send();