Over a million developers have joined DZone.

Getting Started with Spring Social - a Footnote

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

If you’ve seen my last blog, you’ll know that I’ve been talking about writing the smallest and simplest Spring Social application that I could think of that can read and display your user’s private Facebook data.

If, by any chance, you decide to use the Facebook Github sample as inspiration for your own application then you’ll need to register your application with Facebook or other Software as a Service (SaaS) provider. This is something I’ve already mentioned this in my blog on OAuth Admin Steps, which describes how you need to visit https://developers.facebook.com/apps to register your application with Facebook.

In registering your app with Facebook or any other SaaS provider, you’ll be creating an app id/key (also known as a client id) and app secret. These you’ll use in your application as part of the Spring Social Connection Factory. In the case of Facebook they're used by the FacebookConnectionFactory class.
  private static final String appId = "439291719425239";
  private static final String appSecret = "65646c3846ab46f0b44d73bb26087f06";
  @Bean
  public ConnectionFactoryLocator connectionFactoryLocator() {

    logger.info("getting connectionFactoryLocator");
    ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
    registry.addConnectionFactory(new FacebookConnectionFactory(appId, appSecret));
    return registry;
  }


You’ll notice how I’ve hard coded these values for simplicity - after all this is only a sample. I guess that in the ‘real-world’ they’ll be included in a configuration file...

In setting up your Facebook client, there’s only one real Gotcha to be aware of. As well as creating an app id and secret, you’ll also need to define what Facebook calls the ‘Website with Facebook Login’ also known as the ‘Site Url’. This is the redirect URL that tells Facebook where to call back to during the OAuth conversation. In practical terms, for this sample it’s:

http://localhost:8080/facebook/signin/facebook

...which breaks down into your server’s URL:

http://localhost:8080/facebook

... and the request mapping handled by Spring’s ProviderSignInController, which in this case is:

singin/facebook 

...but is more generically defined as:

signin/{providerId} 

where ‘providerId’ is facebook, twitter, linkedin etc.

The image below shows the section of the page that you need to edit:


Note that if you’re developing behind a proxy server, you’ll need to modify this value to match that of your proxy.

As this value needs to be hard-coded into Facebook or other SaaS, then a good approach to take is to create a bundle of applications in the same account, one for each stage of your development each with their own app id, secret and redirect URI. For example:

  1. Development: http://localhost:8080/facebook/signin/facebook
  2. Testing: http://my.company.com:8080/myapp/test/signin/facebook
  3. UAT: http://my.company.com:8080/myapp/uat/signin/facebook
  4. Live: http://my.company.com/myapp/signin/facebook

You can then add the app ids and secrets to a set of property files that are included in your WAR file on a per build type (dev, test, live etc) basis: Maven’s filter properties is very good at sorting this out for you - perhaps I should write a blog on it...


The Facebook sample code is available at: https://github.com/roghughe/captaindebug.git

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Roger Hughes, 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 }}