Grails oauth plugin is an awesome plugin, using this you can integrate your application with several sites that provide oauth support.
Following are the simple steps to integrate LinkedIn in you grails application:
Step 1 : Install the Oauth plugin
Open BuildConfig.groovy and add the following dependency inside plugins section :
Step 2 : Sign up as LinkedIn developer and create a new application
Go to the link and complete the application form. After successfully creating the app you got the API Key and Secret Key.
Step 3 : Add Config values in Config.groovy
Add following code in you Config file.
|
oauth { providers { linkedin { api = org.scribe.builder.api.LinkedInApi key = 'YOUR API Key' secret = 'YOUR Secret Key' callback = "http://localhost:8080/grailsOauthPluginDemo/oauth/linkedin/callback" successUri = "http://localhost:8080/grailsOauthPluginDemo/oauthCallBack/linkedin" failureUri = "http://localhost:8080/grailsOauthPluginDemo/oauthCallBack/failure" } } } grails.linkedin.api.url = "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,date-of-birth)?format=json" |
|
NOTE:- http://localhost:8080/grailsOauthPluginDemo is my server URL, replace it with yours.
Step 4 : Add linkedIn link in your sign up page
Add oauth:connect tag of oauth plugin tag library to create a link to LinkedIn.
<oauth:connect provider="linkedin">LinkedIn</oauth:connect>
Step 5 : Add handler for linkedIn response
Final step is to create action to handle the response returned by the LinkedIn after successful authentication.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
def linkedin() { Token linkedinAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('linkedin')] def linkedInResponse = oauthService.getLinkedInResource(linkedinAccessToken, grailsApplication.config.grails.linkedin.api.url) def linkedinParsedResponse = JSON.parse(linkedInResponse?.getBody()) log.info "linkedinAccessToken = ${linkedinAccessToken}" log.info "linkedInResponse = ${linkedInResponse}" log.info "linkedinParsedResponse = ${linkedinParsedResponse}" log.info "linkedinParsedResponse id = ${linkedinParsedResponse.id}" log.info "emailAddress id = ${linkedinParsedResponse.emailAddress}" User user = User.findByLinkedInId(linkedinParsedResponse.id) if (user) { // If user found with this linkedIn id, authenticate him springSecurityService.reauthenticate(user.username) } else { /* Write code to redirect user to your sign up page. Make sure you persist the linkedIn id for future use */ } } |
|
You can get working code in the repository Grails Oauth Plugin Demo.
Trackbacks/Pingbacks