4
votes

I have a grails application and I want to connect my user's account to their LinkedIn accounts.

So my steps are:

  • Have the user click on a button that redirects to:

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=MY_API_KEY&scope=r_network&state=SOME_TEXT&redirect_uri=MY_REDIRECT_URI

  • Then LinkedIn redirects to the specified redirect_uri and I get the authorization code as a parameter in the response. With that code I do a post to:

https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=MY_AUTHORIZATION_CODE&redirect_uri=SAME_REDIREC_URI_AS_BEFORE&client_id=MY_API_KEY&client_secret=MY_API_SECRET

  • That works like charm! I get the Access Token and I save it in the User domain class together with the expiration date.

Now my issue comes when I want to have a piece of code with the logic to refresh the Access Token before it expires to avoid having the user clicking on the button every now and then. I know of applications where you link your account to LinkedIn and never have to refresh the token again.

In the documentation: http://developer.linkedin.com/documents/handling-errors-invalid-tokens you can find a section called Refreshing Access Tokens that says:

Refreshing an access token is very simple and can happen without an authorization dialog appearing for the user. In other words, it's a seamless process that doesn't affect your application's user experience.

Simply have your application go through the authorization flow in order to fetch a new access token with an additional 60 day life span.

So how can I follow the same process describe above if it starts with a click of the user in a button.

I have tried doing a GET using he HTTPClient class from groovy like follows:

new RESTClient(accessTokenRequestUrl, ContentType.URLENC)

where the accessTokenRequestUrl is the same used above in the button href. This should eventually call my redirect_uri where I use the authorization code to request the access token but it never gets to that point.

I have tried using the RESTClient add-on for Firefox and it works ok but it doesn't if the call is done from within the application.

Any thoughts? Thanks in advance!

Cheers,

Juan

1

1 Answers

1
votes

If you use the Linkedin JavaScript API, then the access token will be automatically refreshed without any user intervention. Make sure inside your initialization string you set authorize: true so that refresh is seamlessly done as follows:

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: YOUR_KEY_HERE
  authorize: true
</script>