Vous êtes sur la page 1sur 4

GettingStarted

Getting Started with implementing SocialAuth Updated Jun 14, 2012 by tar...@brickred.com

Getting Started
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. o Google (show screenshot) http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html o Yahoo (show screenshot) https://developer.apps.yahoo.com/dashboard/createKey.html o Twitter - http://twitter.com/apps o Facebook - http://www.facebook.com/developers/apps.php o Hotmail (show screenshot) - http://msdn.microsoft.com/enus/library/cc287659.aspx o FourSquare - (show screenshot) - https://foursquare.com/oauth/ o MySpace - (show screenshot) - http://developer.myspace.com/Apps.mvc o Linkedin - (show screenshot) - https://www.linkedin.com/secure/developer o Salesforce - (show screenshot) o Yammer - (show screenshot) - https://www.yammer.com/client_applications/new o Mendeley - (show screenshot) - http://dev.mendeley.com/applications/register/ 1. 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. 2. 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-2.3.zip and following are the files that you would need to incorporate in your project from "dist" and "dependencies" directory of SDK.:

Application Type Generic / Struts Application Spring Application Seam Application Using Filter Grails Application socialauth2.3.jar socialauth2.3.jar socialauth2.3.jar socialauth2.3.jar] socialauth2.3.jar

Jars Required Files from dependencies folder Files from dependencies folder Files from dependencies folder Files from dependencies folder Files from dependencies folder socialauth-spring-2.0beta2.jar socialauth-seam-2.0beta1.jar socialauth-filter-2.2.jar socialauth-filter-2.2.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>2.3</version> </dependency>

Add Dependency for spring library if required


<dependency> <groupId>org.brickred</groupId> <artifactId>socialauth-spring</artifactId> <version>2.0-beta2</version> </dependency>

Add Dependency for seam library if required


<dependency> <groupId>org.brickred</groupId>

<artifactId>socialauth-seam</artifactId> <version>2.0-beta1</version> </dependency>

Add Dependency for filter library if required


<dependency> <groupId>org.brickred</groupId> <artifactId>socialauth-filter</artifactId> <version>2.2</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();

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

Developing a JSF or JBoss Seam 2.0 application using socialauth Developing a Struts application using socialauth Developing a Spring MVC application using socialauth Developing a Grails application using socialauth Developing a application with your own framework OR Servlet application 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.

Step by Step JSF or JBoss Seam 2.0 application using socialauth Step by Step Struts application using socialauth Step by Step Spring MVC application using socialauth

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

Vous aimerez peut-être aussi