...
Some endpoint options such as consumerKey
, consumerSecret
or keyLoader
are usually set to the same values on gauth:authorize
and gauth:upgrade
endpoints. The gauth
component allows to configure them on component-level. These settings are then inherited by gauth
endpoints and need not be set redundantly in the endpoint URIs. Here are some configuration examples.
Code Block |
---|
| xml |
---|
| xml |
---|
title | component configuration for a registered web application using the HMAC-SHA1 signature methodxml |
---|
|
<bean id="gauth" class="org.apache.camel.component.gae.auth.GAuthComponent">
<property name="consumerKey" value="example.appspot.com" />
<property name="consumerSecret" value="QAtA...HfQ" />
</bean>
|
Code Block |
---|
| xml |
---|
| xml |
---|
title | component configuration for an unregistered web application using the HMAC-SHA1 signature methodxml |
---|
|
<bean id="gauth" class="org.apache.camel.component.gae.auth.GAuthComponent">
<!-- Google will display a warning message on the authorization page -->
<property name="consumerKey" value="anonymous" />
<property name="consumerSecret" value="anonymous" />
</bean>
|
Code Block |
---|
| xml |
---|
| xml |
---|
title | component configuration for a registered web application using the RSA-SHA1 signature method | xml |
---|
|
<bean id="gauth" class="org.apache.camel.component.gae.auth.GAuthComponent">
<property name="consumerKey" value="ipfcloud.appspot.com" />
<property name="keyLoader" ref="jksLoader" />
<!--<property name="keyLoader" ref="pk8Loader" />-->
</bean>
<!-- Loads the private key from a Java key store -->
<bean id="jksLoader"
class="org.apache.camel.component.gae.auth.GAuthJksLoader">
<property name="keyStoreLocation" value="myKeytore.jks" />
<property name="keyAlias" value="myKey" />
<property name="keyPass" value="myKeyPassword" />
<property name="storePass" value="myStorePassword" />
</bean>
<!-- Loads the private key from a PKCS#8 file -->
<bean id="pk8Loader"
class="org.apache.camel.component.gae.auth.GAuthPk8Loader">
<property name="keyStoreLocation" value="myKeyfile.pk8" />
</bean>
|
...
Here's the minimum setup for adding OAuth to a (non-GAE) web application. In the following example, it is assumed that the web application is running on gauth.example.org
.
Code Block |
---|
| java |
---|
| java |
---|
title | GAuthRouteBuilder.javajava |
---|
|
import java.net.URLEncoder;
import org.apache.camel.builder.RouteBuilder;
public class GAuthRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
// Calback URL to redirect user from Google Authorization back to the web application
String encodedCallback = URLEncoder.encode("https://gauth.example.org:8443/handler", "UTF-8");
// Application will request for authorization to access a user's Google Calendar
String encodedScope = URLEncoder.encode("http://www.google.com/calendar/feeds/", "UTF-8");
// Route 1: A GET request to http://gauth.example.org/authorize will trigger the the OAuth
// sequence of interactions. The gauth:authorize endpoint obtains an unauthorized request
// token from Google and then redirects the user (browser) to a Google authorization page.
from("jetty:http://0.0.0.0:8080/authorize")
.to("gauth:authorize?callback=" + encodedCallback + "&scope=" + encodedScope);
// Route 2: Handle callback from Google. After the user granted access to Google Calendar
// Google redirects the user to https://gauth.example.org:8443/handler (see callback) along
// with an authorized request token. The gauth:access endpoint exchanges the authorized
// request token against a long-lived access token.
from("jetty:https://0.0.0.0:8443/handler")
.to("gauth:upgrade")
// The access token can be obtained from
// exchange.getOut().getHeader(GAuthUpgradeBinding.GAUTH_ACCESS_TOKEN)
// The access token secret can be obtained from
// exchange.getOut().getHeader(GAuthUpgradeBinding.GAUTH_ACCESS_TOKEN_SECRET)
.process(/* store the tokens in context of the current user ... */);
}
}
|
...
To OAuth-enable a Google App Engine application, only some small changes in the route builder are required. Assuming the GAE application hostname is camelcloud.appspot.com
a configuration might look as follows. Here, the ghttp component is used to handle HTTP(S) requests instead of the jetty
component.
Code Block |
---|
| java |
---|
| java |
---|
title | GAuthRouteBuilder | java |
---|
|
import java.net.URLEncoder;
import org.apache.camel.builder.RouteBuilder;
public class TutorialRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
String encodedCallback = URLEncoder.encode("https://camelcloud.appspot.com/handler", "UTF-8");
String encodedScope = URLEncoder.encode("http://www.google.com/calendar/feeds/", "UTF-8");
from("ghttp:///authorize")
.to("gauth:authorize?callback=" + encodedCallback + "&scope=" + encodedScope);
from("ghttp:///handler")
.to("gauth:upgrade")
.process(/* store the tokens in context of the current user ... */);
}
}
|
...
Here's an example how to use an access token to access a user's Google Calendar data with the GData Java library. The example application writes the titles of the user's public and private calendars to stdout
.
Code Block |
---|
| java |
---|
| java |
---|
title | Access token usagejava |
---|
|
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthParameters;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import java.net.URL;
public class AccessExample {
public static void main(String... args) throws Exception {
String accessToken = ...
String accessTokenSecret = ...
CalendarService myService = new CalendarService("exampleCo-exampleApp-1.0");
OAuthParameters params = new OAuthParameters();
params.setOAuthConsumerKey("anonymous");
params.setOAuthConsumerSecret("anonymous");
params.setOAuthToken(accessToken);
params.setOAuthTokenSecret(accessTokenSecret);
myService.setOAuthCredentials(params, new OAuthHmacSha1Signer());
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
System.out.println();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
CalendarEntry entry = resultFeed.getEntries().get(i);
System.out.println(entry.getTitle().getPlainText());
}
}
}
|
...
Maven users will need to add the following dependency to their pom.xml
.
Code Block |
---|
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-gae</artifactId>
<version>${camel-version}</version>
</dependency>
|
...