Use OAuth 2.0 in an Application-Only App to connect to Stream API?


#1

Greetings,

I want to use OAuth 2.0 and an Application-Only app to connect to the stream API, specifically “https://stream.twitter.com/1.1/statuses/filter.json”. Is this possible? I retrieved a bearer token and successfully implemented the example on https://dev.twitter.com/oauth/application-only. However this examples accesses the api.twitter.com endpoint i.e. “https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi”. I want to connect to the stream api. I’ve tried both POST statuses/filter and GET statuses/sample. Both of these attempts returned a 401 Authentication Error.

Am I implementing the http request incorrectly? Does the bearer token that worked for the api.twitter.com call require something more to function with the stream api? Does OAuth 2.0 work only on api.twitter.com and not on stream.twitter.com?

Any assistance is appreciated.
Thank you!

/******************** Code ******************/
The first method attempts to connect to the stream api. The second method retrieves the bearer token that successfully accesses api.twitter.com but returns a 401 message when attempting access to stream.twitter.com.

// Connect to twitter stream api
private void connectToStreamApi(String endPointUrl) {
HttpURLConnection connection = null;

    try {
        URL url = new URL(endPointUrl); 
        connection = (HttpURLConnection) url.openConnection();           
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Host", "stream.twitter.com");
        connection.setRequestProperty("User-Agent", "program name");
        connection.setRequestProperty("Authorization", "Bearer " +                           BEARER_TOKEN); 
        
        BufferedReader br = new BufferedReader(new           InputStreamReader(connection.getInputStream()));
              
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
    catch (MalformedURLException mue) {
         mue.printStackTrace(System.out);
    }
    catch (IOException ioe) {
        ioe.printStackTrace(System.out);
    }
    finally {
        if (connection != null) {
            connection.disconnect();
        }
    }   
}     

// Request a bearer token from server and returns that token as a string
private String getBearerToken(String endPointUrl) {
HttpURLConnection connection = null;
String encodedCredentials = encodeKeyAndSecretIntoBase64(CONSUMER_KEY, CONSUMER_SECRET );

try {
        URL url = new URL(endPointUrl); 
        connection = (HttpURLConnection) url.openConnection();           
        connection.setDoOutput(true);
        connection.setDoInput(true); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Host", "api.twitter.com");
        connection.setRequestProperty("User-Agent", "program name");
        connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
        connection.setRequestProperty("Content-Length", "29");
        connection.setUseCaches(false);
        
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
        wr.write("grant_type=client_credentials");
        wr.flush();
        wr.close();

        return readResponse(connection);
        
}
catch (MalformedURLException e) {
        e.printStackTrace(System.out);
    }
    catch (IOException ieo) {
        ieo.printStackTrace(System.out);
}
finally {
        if (connection != null) {
            connection.disconnect();
        }
}
    
    return new String();
}