Skip to content

Getting Started with implementing SocialAuth

Tarun Nagpal edited this page Sep 16, 2013 · 10 revisions

Step 1. Prerequisites


Authenticating using the external oAuth providers requires that we register our application with the providers and obtain a key/secret from them that will be configured in our application. So following steps are needed to be set up before we can begin.
  1. Public domain - You will need a public domain for testing. You should have a public domain because most of the providers require a public domain to be specified when you register an application with them.
  2. Get the API Keys: You can get the API keys from the following URLs. * Google (show screenshot) - http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html * Yahoo (show screenshot) - https://developer.apps.yahoo.com/dashboard/createKey.html * Twitter - http://twitter.com/apps * Facebook - http://www.facebook.com/developers/apps.php * Hotmail (show screenshot) - http://msdn.microsoft.com/en-us/library/cc287659.aspx * FourSquare - (show screenshot) - https://foursquare.com/oauth/ * MySpace - (show screenshot) - http://developer.myspace.com/Apps.mvc * Linkedin - (show screenshot) - https://www.linkedin.com/secure/developer * Salesforce - (show screenshot) * Yammer - (show screenshot) - https://www.yammer.com/client_applications * Mendeley - (show screenshot) - http://dev.mendeley.com/applications/register/ * Flickr - http://www.flickr.com/services/apps
  • You can now develop the application using keys and secrets obtained above and deploy the application on your public domain. However, most people need to test the application on a local development machine using the API keys and secrets obtained above.
  • We do not recommend it at all, but if you do not want to obtain your own keys and secrets while testing, you can use the keys and secrets that we obtained by registering "opensource.brickred.com" for our demo. Follow the same steps as above but with domain as "opensource.brickred.com" and keys from our sample.

###Step 2. Getting the library
You can either download our SDK and use pre-built jars or use Maven to integrate socialauth in your project. You can download socialauth-java-sdk-4.2.zip and following are the files that you would need to incorporate in your project from "dist" and "dependencies" directory of SDK.:

Application Type Jars Required
Generic / Struts Application socialauth-4.2.jar Files from dependencies folder
Spring Application socialauth-4.2.jar Files from dependencies folder socialauth-spring-2.3.jar
Seam Application socialauth-4.2.jar Files from dependencies folder socialauth-seam-2.1.jar
CDI Application socialauth-4.2.jar Files from dependencies folder socialauth-cdi-2.1.jar
Using Filter socialauth-4.2.jar Files from dependencies folder socialauth-filter-2.4.jar
Grails Application socialauth-4.2.jar Files from dependencies folder socialauth-filter-2.4.jar

Note: If you are not using OpenID provider, in that case you can remove openid4java.jar from dependencies folder.

If you are using Maven, you can configure the pom.xml as follows:

  • Add the repository
<repository>
    <id>sonatype-oss-public</id>
    <url>https://oss.sonatype.org/content/groups/public/</url>
    <releases>
        <enabled>true</enabled>
    </releases>
</repository>
  • Add dependency of core library
<dependency>
  <groupId>org.brickred</groupId>
  <artifactId>socialauth</artifactId>
  <version>4.2</version>
</dependency>
  • Add Dependency for spring library if required
<dependency>
    <groupId>org.brickred</groupId>
    <artifactId>socialauth-spring</artifactId>
    <version>2.3</version>
</dependency>
  • Add Dependency for seam library if required
<dependency>
    <groupId>org.brickred</groupId>
    <artifactId>socialauth-seam</artifactId>
    <version>2.1</version>
</dependency>
  • Add Dependency for filter library if required
<dependency>
  <groupId>org.brickred</groupId>
  <artifactId>socialauth-filter</artifactId>
  <version>2.4</version>
</dependency>

Step 3. Implementation


Using the socialauth.jar consists of two main steps:
  • User chooses provider - Create a page where you ask the user to choose a provider. When the user clicks on a provider, in your handling code you should do the follwing:
    1. Create a instance of !SocialAuthConfig and call load() method to load configuration for providers.
    2. Create a instance of !SocialAuthManager and call setSocialAuthConfig() to set the configuration.
    3. Store !!SocialAuthManager object in session.
    4. Redirect to the URL obtained by calling the function getAuthenticationUrl()

   //Create an instance of SocialAuthConfgi object
   SocialAuthConfig config = SocialAuthConfig.getDefault();

  //load configuration. By default load the configuration from oauth_consumer.properties. 
  //You can also pass input stream, properties object or properties file name.
   config.load();

  //Create an instance of SocialAuthManager and set config
  SocialAuthManager manager = new SocialAuthManager();
  manager.setSocialAuthConfig(config);

  //URL of YOUR application which will be called after authentication
  String successUrl = "http://opensource.brickred.com/socialauthdemo/socialAuthSuccessAction.do";

  // get Provider URL to which you should redirect for authentication.
  // id can have values "facebook", "twitter", "yahoo" etc. or the OpenID URL
  String url = manager.getAuthenticationUrl(id, successUrl);

  // Store in session
  session.setAttribute("authManager", manager);
  • Provider redirects back - When you redirect the user to the provider URL, the provider would validate the user, either by asking for username / password or by existing session and will then redirect the user back to you application URL mentioned above, i.e. "http://opensource.brickred.com/socialauthdemo/socialAuthSuccessAction.do". Now you can obtain any profile information using the following code

  // get the auth provider manager from session
  SocialAuthManager manager = (SocialAuthManager)session.getAttribute("authManager");

  // call connect method of manager which returns the provider object. 
  // Pass request parameter map while calling connect method.
   Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request); 
   AuthProvider provider = manager.connect(paramsMap);

  // get profile
  Profile p = provider.getUserProfile();

  // you can obtain profile information
  System.out.println(p.getFirstName());

  // OR also obtain list of contacts
  List<Contact> contactsList = provider.getContactList();
    	

Step 4. Use Plugin


SocialAuth library now has plugin for getting feeds from Facebook, Twitter and Linkedin. It also has plugin for getting Albums from Facebook and Twitter.

if (provider.isSupportedPlugin(org.brickred.socialauth.plugin.FeedPlugin.class)) {
    FeedPlugin p = provider.getPlugin(org.brickred.socialauth.plugin.FeedPlugin.class);
    List<Feed> feeds = p.getFeeds();
}
if (provider.isSupportedPlugin(org.brickred.socialauth.plugin.AlbumsPlugin.class)) {
    AlbumsPlugin p = provider.getPlugin(org.brickred.socialauth.plugin.AlbumsPlugin.class);
    List<Album> albums = p.getAlbums();
}

That is all you have to do, really. We have the following tracks available on how to use SocialAuth with different frameworks.

If you are new to these frameworks or face issues in following the above guides, we also have step-by-step guides available.

Please keep in mind that all features may not be implemented in all providers, for example, you can’t update your status on Gmail and you can’t import contacts from Facebook yet. Some of the limitations are inherent, for example Twitter doesn’t give out the email address while giving other user details.

Please report any issues and we promise to get back.