-
Notifications
You must be signed in to change notification settings - Fork 141
Getting Started with implementing SocialAuth
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.
- 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.
- 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>
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:
- Create a instance of !SocialAuthConfig and call load() method to load configuration for providers.
- Create a instance of !SocialAuthManager and call setSocialAuthConfig() to set the configuration.
- Store !!SocialAuthManager object in session.
- 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();
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.
- GettingStartedWithSeam Developing a JSF or JBoss Seam 2.0 application using socialauth
- StrutsSample Developing a Struts application using socialauth
- GettingStartedWithSpring Developing a Spring MVC application using socialauth
- GettingStartedWithGrails Developing a Grails application using socialauth
- GettingStartedWithYourOwnFramework Developing a application with your own framework OR Servlet application
- GettingStartedWithSocialAuthFilter Developing a application using socialauth filter
If you are new to these frameworks or face issues in following the above guides, we also have step-by-step guides available.
- SeamSample Step by Step JSF or JBoss Seam 2.0 application using socialauth
- StrutsSample Step by Step Struts application using socialauth
- SpringSample Step by Step Spring MVC application using socialauth
- CDISample Step by Step CDI application using socialauth
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.