Over a million developers have joined DZone.

Simple Camel Configuration of a Twitter Endpoint

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

I was asked just how my got the Twitter Stream working in my new Camel based project and how I managed the credentials.

The Twitter endpoint works like a dream and this is essentially what my code looks like. All you need is a secrets.properties file in alongside your java file.
/**
 * A Camel Java DSL Router
 */
public class MyRouteBuilder extends RouteBuilder {

	private static final ResourceBundle SECRETS = ResourceBundle
			.getBundle("myproject.secrets");

	/**
	 * Let's configure the Camel routing rules using Java code...
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void configure() throws UnsupportedEncodingException {

		configureAccess();

		String twitter = "twitter://streaming/filter?type=event&keywords="
				+ URLEncoder.encode("london", "utf8");

		from(twitter).filter(body().isInstanceOf(Status.class)).addAllSortsOfStuffHere().

	}

	private void configureAccess() {
		// setup Twitter component
		TwitterComponent tc = getContext().getComponent("twitter",
				TwitterComponent.class);
		tc.setAccessToken(SECRETS.getString("ACCESS_TOKEN"));
		tc.setAccessTokenSecret(SECRETS.getString("ACCESS_TOKEN_SECRET"));
		tc.setConsumerKey(SECRETS.getString("CONSUMER_KEY"));
		tc.setConsumerSecret(SECRETS.getString("CONSUMER_SECRET"));
	}
}



Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:

Published at DZone with permission of Col Wilson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}