SeamFramework.orgCommunity Documentation

Chapter 82. Seam Social in 5 minutes

82.1. Declaring an OAuth Configuration
82.2. Configuration with a producer method or a bean definition
82.3. Injecting the Service Bean into your code
82.4. Request the OAuth authorization URL
82.5. Set the verifier and initiate connection
82.6. Send request to the service

The Web example app is quite simple and gives a good idea of the possibilities with Seam Social. The main steps you need to take to use Seam Social are:

Should you need to fully understand each step, the complete OAuth lifecycle can be found here or here.

To consume an OAuth service you need to declare an application on the service platform (i.e. for Twitter you can do this at https://dev.twitter.com/apps/new. The declaration of an application is done with the @OAuthApplication annotation which must contain at least:

  • An OAuth API public key
  • An OAuth API private/secret key

If you don't know what this is about, please refer to the OAuth concepts in your service documentation.

To use an OAuth Service Bean in Seam Social you need to provide the following configuration information by producing the right OAuthService bean:

  • Via a producer method or subclassing
  • Via an XML configuration (Using Solder's bean configuration feature).

The simplest way to configure your service is to create a producer method like so:

   @OAuthApplication(apiKey = "FQzlQC49UhvbMZoxUIvHTQ", apiSecret = "VQ5CZHG4qUoAkUUmckPn4iN4yyjBKcORTW0wnok4r1k")
    @Twitter
    @Produces
    TwitterService twitterServiceProducer(TwitterService ts) {
        return ts;
    }

You can also create a bean by subclassing the implementation of the service like this:

  @OAuthApplication(apiKey = "FQzlQC49UhvbMZoxUIvHTQ", apiSecret = "VQ5CZHG4qUoAkUUmckPn4iN4yyjBKcORTW0wnok4r1k")
    @Twitter
    public class MyTwitterBean extends TwitterServiceJackson {
    
…
}

The API key and API secret are provided by the service you want to consume (here Twitter). You can use the values above since they're coming from the "Seam Social" Twitter application. The callback depends on your application - it's the URL that will collect the OAuth verifier.

You can now inject the bean with the right service qualifier:

@Named
@SessionScoped
public class mySessionBean implements Serializable {
    ...
    @Inject
    @Twitter
    TwitterService service;
    ...
}

You can now ask for the authorization URL for your service:

String authURL = service.getAuthorizationUrl();

Calling this URL will bring the user on the service connection page and right delegation for the application. If the user gives rights to the application to use the service on their behalf the service will send you back a special code (verifier) that you must inject into the service to initiate the connection.

As the verifier comes back to the application after an action of the final user, you have to set up a servlet or a JSF page (the URL of which is the callback URL you configured when you set up the service) to catch it and add it to the current session. Here is an example with JSF:

<f:metadata>
    <f:viewParam name="#{mySessionBean.twitterService.verifierParamName}"
        value="#{mySessionBean.twitterService.verifier}"
                 required="true"
                 requiredMessage="Error with Twitter. Retry later"/>
    <f:event type="preRenderView"
        listener="#{mySessionBean.twitterService.initAccessToken()}"/>
</f:metadata>

The service is now connected - you have an access token.

You can now use the service with your rights:

TwitterProfile user = twitter.getMyProfile();
String fullName = user.getFullName();