Skip to main content
Last updated September 07, 2012 13:46, by erwin_lodgon


dalicore-social is the dalicore module that allows setting up an oauth connection with third party social network systems. It does not contain user interface components. At the moment, dalicore-social has support for the following external networks: facebook, twitter, google and dalicore-oauth. dalicore-social was previously known as dalicore-externalnetwork.

Using an ExternalNetwork

An example on how to use dalicore-social can be found in the demos directory. Basically it always boils down to having two endpoints in your application:

Initiate connection
this initiates the connection with the social network by obtaining the request token and redirecting the user to the authentication page afterwards
Handle callback
this handles the callback url where the user will be redirected to after authorizing the service consumer and takes care of getting the access token from the service provider

The following sections explain these steps in a bit more detail. For the most convenient integration with dalicore-social, we strongly recommend you to implement your web application using Jersey.

Initiate connection

Initiating the connection with the social network requires you to call the connect method on the social network. This will return a Jersey Response object that redirects the user to the authentication page of the social network. In the following code listing we provide a Jersey method that can for instance be called with the following URL: http://localhost:8080/mycoolapp/rest/connect.

public Response connect() {
  // create an instance of the TwitterExternalNetwork class
  TwitterExternalNetwork twitter = new TwitterExternalNetwork(YOUR_TWITTER_CONSUMER_KEY, YOUR_TWITTER_CONSUMER_SECRET);
  // fetch a request token and return the response object
  return twitter.connect("http://localhost:8080/mycoolapp/rest/callback");

Handle callback

When the user finally authorized your application on the social network, the social network will redirect the user back to the provided callback URL. The callback will contain the oauth request token and a verifier which you need to obtain the access token. Here is a Jersey method that handles the callback:

public void callback(@Context HttpServletRequest request,
          @QueryParam("oauth_token") String requestToken,
          @QueryParam("oauth_verifier") String verifier) {
  // create an instance of the TwitterExternalNetwork class
  TwitterExternalNetwork twitter = new TwitterExternalNetwork(YOUR_TWITTER_CONSUMER_KEY, YOUR_TWITTER_CONSUMER_SECRET);

  // obtain the access token from the external network
  ExternalToken accessToken = twitter.callback(requestToken, verifier);

  // you can now put the access token on the active session for example
  // and start making requests to the external network
  request.getSession().setAttribute("accessToken", accessToken);

Using the access token

Now that you have successfully obtained your access token from the external network, you can start making valid oauth calls to the external network. Again, the easiest way to do this is by using the Jersey Client library. For example, the following code will get some user information from the user's twitter account:

// get the access token from the session
ExternalToken accessToken  = (ExternalToken) request.getSession().getAttribute("accessToken");

// create a jersey client
Client client = Client.create();

// create the oauth filter for the jersey web resource
OAuthParameters params = new OAuthParameters().signatureMethod(HMAC_SHA1.NAME).
OAuthSecrets secrets = new OAuthSecrets().tokenSecret(accessToken.getSecret()).consumerSecret(consumerSecret);
OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);

// create the jersey web resource and add the oauth filter
WebResource wr = client.resource("");

// call the URL to get twitter profile information
ClientResponse clientResponse = wr.path("1/account/verify_credentials.xml").get(ClientResponse.class);
if (clientResponse.getStatus() == 200) {
  String xmlWithTwitterProfileInfo = clientResponse.getEntity(String.class);

Example implementation

A more elaborate explanation and implementation of dalicore-social can be found on Joeri's blog entry: Easy OAuth using DaliCore and Glassfish: the service consumer.

Please Confirm