DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • The Rise of Biometric Security: Protecting Data in the Future of Cybercrime
  • When To Boost Your Authentication Strategy: Signs for Security Enhancement
  • Identity and Access Management Best Practices
  • Configuring SSO Using WSO2 Identity Server

Trending

  • Next.js vs. Gatsby: A Comprehensive Comparison
  • Bad Software Examples: How Much Can Poor Code Hurt You?
  • The Systemic Process of Debugging
  • How To Verify Database Connection From a Spring Boot Application
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Grails OAuth and LinkedIn APIs

Grails OAuth and LinkedIn APIs

Matt Raible user avatar by
Matt Raible
·
Dec. 23, 09 · Interview
Like (0)
Save
Tweet
Share
16.49K Views

Join the DZone community and get the full member experience.

Join For Free
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.

From http://raibledesigns.com/rd/entry/grails_oauth_and_linkedin_apis

Grail (web browser) authentication security

Opinions expressed by DZone contributors are their own.

Related

  • The Rise of Biometric Security: Protecting Data in the Future of Cybercrime
  • When To Boost Your Authentication Strategy: Signs for Security Enhancement
  • Identity and Access Management Best Practices
  • Configuring SSO Using WSO2 Identity Server

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: