3

I am trying to use the new oAuth 2.0 plugin for Grails to consume LinkedIn resources. With my code, I am able to get to LinkedIn authorization page where I can grant my app permission to access my LinkedIn account info.

The problem is that once I hit the continue button, it does not redirect back to my app. instead, it goes to a page that says "You have successfully authorized XXXX. Please return to your application and enter the following security code to grant access: some number"

How do I get this to redirect back to my app?

My settings are:

Config.groovy

oauth {
        providers{
            linkedin {              
                api="org.scribe.builder.api.LinkedInApi"
                key = 'my key'
                secret = 'my secret'
                successUri = '/linkedinProfile/success'
                failureUri = '/linkedinProfile/failed'
                callback = "http://localhost:8080/myApp/secure/linkedinProfile/success"
            }
        }
    } 

my gsp view:

<oauth:connect provider="linkedin">Connect to linkedin</oauth:connect>

my linked in developer account:

Website URL: http://localhost:8080/myApp OAuth Redirect URL: http://localhost:8080/myApp/secure/linkedinProfile/success

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
jason
  • 3,821
  • 10
  • 63
  • 120

2 Answers2

3

well, if anyone else needs this, here's what i did:

I have three files, the view, the start controller, and the end controller.

on the view, i have a link like this:

  <g:link action="registerOnLinkedIn" controller="linkedinProfile" >connect </g:link>

where I have this method:

        String apiKey =:myKey"
String apiSecret="mySecret"
String callBackUrl="http://localhost:8080/myApp/secure/mySub/success"

    def registerOnLinkedIn = {  

        Token linkedInAccessToken=null;
            OAuthService service=new ServiceBuilder()
            .provider(LinkedInApi.class)
            .apiKey(apiKey)
            .apiSecret(apiSecret)
            .callback(callBackUrl)
            .build();

            Token requestToken = service.getRequestToken();
            String authUrl = service.getAuthorizationUrl(requestToken);
            session['REQUEST_TOKEN'] = requestToken
            redirect(url: authUrl)
}
    def success ={
    String v = params.oauth_verifier
    String r=  session['REQUEST_TOKEN']

    linkedInXmlService.getXmlStream(v,session['REQUEST_TOKEN'])

}

When the user clicks on the link, they are sent to that method, which creates a redirect url. The redirect url is linkedIn's authorization page, where the user can accept the app. once accepted, they are redirected to the success method, which redirects to a service.

The service gets the verifier and token and sends a request to the linkedin API. the bulk of it is here:

    def apiUrl = "http://api.linkedin.com/v1/people/~:(" +
"id," + 
"picture-url," +    
"site-standard-profile-request," +
"first-name," +
"date-of-birth," +
"last-name," +
"industry," +   
"location," +
"educations," + 
"positions:(id,title,summary,start-date,end-date,is-current,company)," +
"skills:(id,skill:(name),proficiency:(level),years:(name))," +
"connections:(id,industry,first-name,last-name,site-standard-profile-request,headline,location,positions,educations,date-of-birth,picture-url,skills:(id,skill:(name),proficiency:(level),years:(name)))" + 
")"


    public void getXmlStream(String ver, rt)
{
    String accessTokenKey=""
    String accessTokenSecret=""

    String xmlString =""
    OAuthService service=new ServiceBuilder()
    .provider(LinkedInApi.class)
    .apiKey(apiKey)
    .apiSecret(apiSecret)
    .build();

    Verifier v = new Verifier(ver);

    Token accessToken = service.getAccessToken(rt, v);
    accessTokenSecret = accessToken.secret
    accessTokenKey = accessToken.token


    OAuthRequest request = new OAuthRequest(Verb.GET, apiUrl);
    service.signRequest(accessToken, request); // the access token from step 4
    Response response = request.send();
    xmlString=response.getBody();
    log.debug (xmlString)
    processData(xmlString, accessTokenKey, accessTokenSecret)

}
jason
  • 3,821
  • 10
  • 63
  • 120
0

The correct way to do this with the plugin is to specify a callback property in your linkedin configuration.

I'm still learning this, so other things in my configuration may be wrong. However, the callback parameter solves this.

oauth {
providers {
    linkedin {
        api = org.scribe.builder.api.LinkedInApi
        key = 'XXX'
        secret = 'XXX'
        successUri = "/oauth/linkedin/callback"
        failureUri = "/oauth/linkedin/error"
        callback = "https://localhost:8443/myapp/oauth/linkedin/callback"
        scope = "w_messages"
    }
}   
debug = true
}
JSager
  • 1,420
  • 9
  • 18
  • specifically, I don't think I have the successUri and failureUri properties set properly... I'm new to this, but regardless it solved the problem you ran into by setting the callback property. – JSager Nov 13 '12 at 00:52