Grails OAuth and LinkedIn APIs
Since then, a few folks have asked how I did it. Since code speaks louder than words, I took some time and 1) verified the oauth plugin works as expected and 2) created an example application demonstrating functionality. You can find the results in my fork of grails-oauth on GitHub. You can also view the example online.
Below is a quick tutorial explaining how to integrate LinkedIn into your Grails application.
- Download and install Grails 1.1.2.
- Run grails create-app to create your application.
- Add the following to the bottom of grails-app/conf/Config.groovy:
oauth {You can get your consumer.key and consumer.secret at https://www.linkedin.com/secure/developer. Make sure to set the OAuth Redirect URL to http://localhost:8080/{your.app.name}/oauth/callback for testing.
linkedin {
requestTokenUrl="https://api.linkedin.com/uas/oauth/requestToken"
accessTokenUrl="https://api.linkedin.com/uas/oauth/accessToken"
authUrl="https://api.linkedin.com/uas/oauth/authorize"
consumer.key="XXX"
consumer.secret="XXX"
}
} - Download the oauth-plugin, extract it and build it using grails package-plugin. Install it in your project using grails install-plugin path/to/zip.
- Add a link to the GSP you want to invoke LinkedIn Authentication from:
<g:oauthLink consumer='linkedin' returnTo="[controller:'profile']">
Login with LinkedIn
</g:oauthLink> - Create grails-app/controllers/ProfileController.groovy to access your LinkedIn Profile.
class ProfileController {
def apiUrl = "http://api.linkedin.com/v1/people/~"
def oauthService
def index = {
if (session.oauthToken == null) {
redirect(uri:"/")
}
if (params?.apiUrl) apiUrl = params.apiUrl
def response = oauthService.accessResource(
apiUrl, 'linkedin', [key:session.oauthToken.key, secret:session.oauthToken.secret], 'GET')
render(view: 'index', model: [profileXML: response, apiUrl: apiUrl])
}
def change = {
if (params?.apiUrl) {
println("Setting api url to " + params.apiUrl)
apiUrl = params.apiUrl
}
redirect(action:index,params:params)
}
} - Create grails-app/views/profile/index.gsp to display the retrieved profile and allow subsequent API calls.
<html>
<head><title>Your Profile</title></head>
<body>
<a class="home" href="${createLinkTo(dir:'')}">Home</a>
<g:hasOauthError>
<div class="errors">
<g:renderOauthError/>
</div>
</g:hasOauthError>
<g:form url="[action:'change',controller:'profile']" method="get">
Your LinkedIn Profile:
<textarea id="payload" style="width: 100%; height: 50%; color: red">${profileXML}</textarea>
<p>
<g:textField name="apiUrl" value="${apiUrl}" size="100%"/>
<br/>
<g:submitButton name="send" value="Send Request"/>
</p>
</g:form>
</body>
</html> - Start your app using grails run-app and enjoy.
As mentioned earlier, you can download the grails-oauth-example or view it online.
One improvement I'd like to see is to simplify the parsing of XML into a Profile object, much like the linkedin gem does for Rails.
If you're interested in learning more about LinkedIn and OAuth, I encourage you to checkout Taylor Singletary's presentation LinkedIn OAuth: Zero to Hero.
Update: I updated the oauth-plugin so it's backwards-compatible with OAuth 1.0 and added Twitter to the example application to prove it. If you're seeing "Cannot invoke method remove() on null object", it's likely caused by your redirect URL pointing to an application on a different domain.
From http://raibledesigns.com/rd/entry/grails_oauth_and_linkedin_apis
- Login or register to post comments
- 8965 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Enes Akar replied on Tue, 2010/01/05 - 7:58pm
great!!! thanks.
And why don't you upgrade the plugin currently located at grails plugin repository. I could not make the old one work with linkedin.
Matt Raible replied on Thu, 2010/02/04 - 9:52am
in response to: enes1177
Murali Gandham replied on Wed, 2011/01/19 - 1:10pm
Great Article.
I have a question though, In the Groovy.config, how do I specify the LinkedIn user name and password ? Basically I have a page, where I let the user enter his LinkedIn credentials and then I would like to use these credentials to authorize with LinkedIn ? So really I would like to dynamically send in the linked-in credentials of the user via OAuth to LinkedIn for authorization.
Any ideas ?
Roshan Dawrani replied on Mon, 2011/07/25 - 6:53am