Using Social Media Integration in Red Hat JBoss Portal 6.1 Beta

September 16, 2013

Portal

Social Portal

JBoss Portal 6.1 was designed with social media integration in mind to tailor use cases that are relevant in today’s web-based applications. For example, when configured, JBoss Portal can let new users to sign up and register by using Facebook, Twitter, or Google+.

Register with Social Media Account

New JBoss Portal 6.1 users can register a new account using Facebook, Twitter, or Google+.

Once signed up, JBoss Portal will automatically pre-populate user information that was retrieved from the corresponding Social Media account, as authorized by the user.

Subsequently, users can sign in directly using the Social Media Account.

Registration with Social Media account.

User can register and sign-in with social media account.

Once signed in, the user can also decide to link or unlink existing Social Media accounts with JBoss Portal! For example, if the a user is currently registered with a Facebook account, and later started using Twitter, the user can link Twitter to the existing Portal user.

Similarly, if a user no longer wishes to stay connected/linked with one of the Social Media providers, the user can disconnect/unlink the account.

Link and Unlink

Registered user can link and unlink their Social Media accounts.

Last, if not least, once a user is connected to a Social Media account, and provided proper authorizations, your JBoss Portal can then provide additional personalized services and/or provide tighter integration with social media.

Some of the popular use cases can be:

  • Activity Stream / Status update from user actions in the Portal
  • Retrieve user profile to provide customized experiences
  • Invite user’s friends to join your Portal website

Enabling Social Media Integration

Enabling Social Media integration in JBoss Portal 6.1 is easy. But first, you’ll need to get additional information from the Social Media provider, such as a Client ID and a Client Secret. These two values will identify your portal application with the corresponding Social Media provider. For more information about obtaining Client ID and Client Secret, please refer to Facebook, Twitter, and/or Google+’s Developer oriented websites.

Once Client ID and Client Secrets are obtained, you can enable corresponding Social Media integration within Portal.

The configuration file is located in:

$JPP_HOME/standalone/configuration/gatein/configuration.properties

Towards the end of this file, you’ll find a number of sections in the form of:

gatein.oauth.[provider].enabled=...
gatein.oauth.[provider].clientId=...
gatein.oauth.[provider].clientSecret=...
gatein.oauth.[provider].redirectURL=...
gatein.oauth.[provider].scope=...

The most important configurations are: enabled, clientId, clientSecret, and scope.

First of all, set enabled to true:

gatein.oauth.[provider].enabled=true

Secondly, configure the clientId, and clientSecret:

gatein.oauth.[provider].clientId=...
gatein.oauth.[provider].clientSecret=...

Last, if not least, configure scope. Scope is especially important – this literally specifies how much authorization would you like to request with the Social Media provider. The available scopes also differs per provider. So for example, if you are configuring Facebook integration, and would like to access user’s email address, friend list, as well as posting status updates on the user’s behave, then you may want to configure the scope as following:

gatein.oauth.facebook.scope=email,publish_actions,publish_stream,status_update,read_friendlists

Under the Hood – OAuth and Token

Under the hood, JBoss Portal uses standard OAuth mechanism to interact with Social Media providers to authenticate the user, and obtaining proper authorizations to retrieve user information or trigger an action on the user’s behave.

JBoss Portal does not include client libraries to interact with the Social Media providers, as each provider has different APIs, and thus, different client libraries can be used in your Portal application.

The important thing, though, is to be able to retrieve the access token – and you can do this via JBoss Portal’s OAuthProvider API.

First, use Portal’s Public API to get an instance of OAuthProvider:

OAuthProvider googleProvider = PortalRequest.getInstance().getPortal().getOAuthProvider(OAuthProvider.GOOGLE);
if (googleProvider == null) {
    // Google+ integration was not enabled in configuration.properties
}

From the OAuthProvider, you can then get the AccessToken:

AccessToken accessToken = googleProvider.loadAccessToken(renderReq.getRemoteUser());
if (accessToken == null) {
    // User has not linked with a Google+ account
}

From the AccessToken, you will be able to check whether certain authorzation/scope has already been obtained – if not, you can initiate process to do obtain the authorization.

String availableScopes = accessToken.getAvailableScopes();
if (!availableScopes.contains("someScope")) {
  // User has linked with Google+ account
  // but we don't have proper scope/authorization
  // we can kick start the scope authorization by calling startOAuthWorkFlow
  googleProvider.startOAuthWorkflow("someScope");
}

If you know you have authorization to the scope,  you can then obtain a reference of a client API object – or use any client API that simply requires a Access Token string.

Plus service = googleProvider.getAuthorizedSocialApiObject(accessToken, Plus.class);

OAuthProvider API gives you full access to how JBoss Portal manages the Access Token as well as verifying token availability, authorized scopes, etc. In most portlets, it may be sufficient to use a re-usable Portlet Filter to check for scopes needed by the portlet, as well as displaying appropriate error messages and to request necessary scopes.

Detailed example of the Portlet Filter can be found in GitHub.

MDBlog Example

MDBlog (MarkDown Blog) portlet was developed to demonstrate some of the latest features of JBoss Portal 6.1.  At a high-level, MDBlog is a portlet that allows user to create and edit Blog articles by using MarkDown template language.

Once a Blog is saved, it will use Portal’s Public API to determine Blog article’s URL. It will also generate a short preview snippet that can be posted onto a user’s activity stream. Finally, it’ll post to a Facebook user’s stream. The core piece of code is located in SocialService Java class.

MDBlog Status Update

When saving an article, the portlet can publish the latest update to Facebook.

Let’s take a look in more detail. First, use the new Navigation API to generate a  URL (We’ll look at Navigation API in-depth in future article):

String link = config.getBaseUrl() + portalRequest.getURIResolver().resolveURI(portalRequest.getSiteId()) +
 portalRequest.getNodePath();

Then generate a snippet of the Blog article to a maximum 200 characters:

String description = Jsoup.parse(processor.markdown(blog.getContent())).text();
 description = ellipsis(description, 200);

Finally, use FBClient to post it!

FacebookClient facebookClient = new DefaultFacebookClient(
    accessToken.getAccessToken());

FacebookType publishMessageResponse = facebookClient.publish(
    "me/feed", FacebookType.class,
    params.toArray(new Parameter[] {}));

Try it out!

,

About Ray Tsang

Ray has worked with technology all his career. Ray was a manager at a global consulting company before taking time off to backpack around Asia. Ray joined Red Hat at the beginning of 2012 as a Solution Architect focusing on Red Hat Middleware products including JBoss Data Grid and JBoss Portal Platform.

View all posts by Ray Tsang

3 Comments on “Using Social Media Integration in Red Hat JBoss Portal 6.1 Beta”

Trackbacks/Pingbacks

  1. Registering Again … (Sigh) | Collaborative Planning & Social Business - September 25, 2013

    […] article on how to configure JBoss to use external authentication shows the […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 105 other followers

%d bloggers like this: