I had a problem while using Streaming API in Java. Visited many sites, but unable to locate problem with code.
Here's the code i'm using :
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);
}
}
}
When i run this program with debugger, i found that exception occur whis code reaches to the end on line shown below.
And Exception is :
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)
I'm using Maven and my pom.xml is :
<?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>
I would be very thankful if someone help me to locate this problem.
I think that problem is here, you created
OAuthRequest request1 = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL, service);
but in code beelow you using
Response response1 = request.send();
instead of
Response response1 = request1.send();