Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Grails OAuth and LinkedIn APIs

Back in November, I wrote about how to talk to LinkedIn APIs with GWT. A week later, I figured out how to do it with Grails and contributed a patch to the grails-oauth plugin.

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.

  1. Download and install Grails 1.1.2.
  2. Run grails create-app to create your application.
  3. Add the following to the bottom of grails-app/conf/Config.groovy:
    oauth {
        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"
        }
    }
    
    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.
  4. 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.
  5. 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>
    
  6. 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)
        }
    }
    
  7. 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>
    
  8. 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.

Posted in Java at Dec 22 2009, 03:37:57 PM MST 7 Comments
Comments:

[Trackback] This post was mentioned on Twitter by mraible: Grails OAuth and LinkedIn APIs: http://raibledesigns.com/rd/entry/grails_oauth_and_linkedin_apis #grails #oauth #linkedin

Posted by uberVU - social comments on December 22, 2009 at 06:36 PM MST #

We're having a similar problem. We get intermittent failure of OAuth from grails. We get a NullPointerException: 2010-04-30 19:30:58,245 ERROR [GrailsExceptionResolver] (http-8080-32) [] ip-10-244-51-228/10.244.51.228 Cannot invoke method remove() on null object

java.lang.NullPointerException: Cannot invoke method remove() on null object
        at OauthController$_closure2.doCall(OauthController.groovy:70)

when doing:

def returnController = params.remove('return_controller')?:session.cbparams.remove('return_controller')

Apparently the session is null at this point. This only happens occasionally, and the user can usually recover by trying again. It is a mystery why it is happening, though.

Posted by 216.49.181.254 on May 07, 2010 at 05:07 PM MDT #

(I forgot to include my contact information). So we get all the way to where the callback is called, but then the session is null when we try to access it.

Any ideas?

Posted by ranbo on May 07, 2010 at 05:09 PM MDT #

Very nice post. Easy to follow. Worked like a charm.

Posted by Rajiv on January 15, 2011 at 12:22 PM MST #

Was so easy to do (once I read this), even a Groovy/Grails newbie (i.e. me) could make it work! THANKS!!

Posted by Nathan Crause on July 25, 2011 at 10:08 AM MDT #

Hi, Nearly a useful article :-) I am trying to figure out whether it is possible to do with LinkedIn the equivalent of what you do in this article using OAuth 2.0 2-legged authentication - first get an authorization code, then an access token, before accessing their API. Any inputs will be highly appreciated. rgds, Roshan

Posted by Roshan Dawrani on July 25, 2011 at 10:08 AM MDT #

i try to do follow your example on grails 2.0.1 ... have a small change when i call form login to linkedin ... and i recieved the error when controller do this code

def response = oauthService.accessResource(
                apiUrl, 'linkedin', [key:session.oauthToken.key, secret:session.oauthToken.secret], 'GET')

the error is "cannot get property 'key' on null value"

please help !!!

Posted by Hien Vu on October 30, 2012 at 09:44 AM MDT #

Post a Comment:
  • HTML Syntax: Allowed