Implement SocialAuth using Grails
Posted By : Sumit Rathi | 27-Apr-2014
While working on a project, I came across a requirement where I had to get information from a social networking service such as Facebook, Twitter or Google+ for web application. There are so many libraries to implement oAuth and OpenID for different social network service. I did some research and found one common api for each social network service i.e. SocialAuth.
SocialAuth is a Java library. You can download latest java library form here.
Why we use SocialAuth?
1. There are many libraries for implementing Open ID but it is difficult to choose one that will do the integration quickly with the providers you want.
2. Even after implementing social auth using different library, it does not work out of the box for all providers. There are always certain things specific to a certain provider. For example the scopes are different as well as some steps in authorization may be different.
How SocialAuth works?
1. Create your developer app account and get API keys from the provider like Facebook, Google+ etc.
2. Make a request for authentication using SocialAuth library.It will redirect the user to Facebook, Google+ etc website where they enter the credentials.
3. The Provider redirect to the user back to your application with a token.
4. You can pass this token to the SocialAuth library to get information about the user.
Steps to implement SocialAuth
1. Make a properties file for consumer_key and consumer_secret key like oauth_counsuemr.properties.
#googleplus googleapis.com.consumer_key = ----------- googleapis.com.consumer_secret = ---------- #yahoo api.login.yahoo.com.consumer_key = -------------- api.login.yahoo.com.consumer_secret = ------------------- #twitter twitter.com.consumer_key = --------------- twitter.com.consumer_secret = ----------------------- #facebook graph.facebook.com.consumer_key = ---------------- graph.facebook.com.consumer_secret = ------------------- #hotmail #consent.live.com.consumer_key = -------------- #consent.live.com.consumer_secret = ----------------- #LinkedIn api.linkedin.com.consumer_key = --------- api.linkedin.com.consumer_secret = -----------------
2. Make an Action for Authentication.
def authenticate() {
SocialAuthConfig config = SocialAuthConfig.getDefault() //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, url successUrl= grailsApplication.config.grails.serverURL +"/socialAuthentication/getToken" // get Provider URL to which you should redirect for authentication. // id can have values "facebook", "twitter", "yahoo" etc. or the OpenID URL url = manager.getAuthenticationUrl(params.id, successUrl) // Store in session session.setAttribute("authManager", manager) redirect(url) }
3. Make an Action to get token information
def getToken() {
try{ // get the social auth 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. AuthProvider provider = manager.connect(SocialAuthUtil.getRequestParametersMap(request)) // get profile Profile profileInfo = provider.getUserProfile(); log.debug"profileInfo "+profileInfo // do your operation here }catch(SocialAuthManagerStateException exception){ log.error("Exception occurs while connecting with SocialAuthManager--->"+exception.getMessage()) }catch(Exception e){ log.error("Exception --->"+e.getMessage()) }
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sumit Rathi
Sumit is a highly experienced Backend Developer with a diverse technological background. He possesses an extensive understanding of the latest technologies and has practical expertise in Core Java, Spring Boot, Hibernate, as well as relational databases like MySQL and PostgreSQL. His proficiency extends to API implementations, Microservices, and Web Services development. Sumit actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including MyHelpa, NOWcast Church Engagement Development, OptaPlanner - Employee Rostering, Optaplanner customization for a medical practice, Handymen Application Upgradation II, LabVue, and more. He showcases exceptional analytical skills and a creative mindset. Furthermore, he has a passion for reading books and exploring new technologies and innovations.