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

4

2 回答 2

3

好吧,如果其他人需要这个,这就是我所做的:

我有三个文件,视图、开始控制器和结束控制器。

在视图中,我有一个这样的链接:

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

我有这个方法的地方:

        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'])

}

当用户点击链接时,他们会被发送到该方法,该方法会创建一个重定向 url。重定向 url 是linkedIn 的授权页面,用户可以在其中接受应用程序。一旦被接受,它们将被重定向到成功方法,该方法重定向到服务。

服务获取验证者和令牌并向linkedin API 发送请求。大部分在这里:

    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)

}
于 2012-09-20T22:41:39.933 回答
0

使用插件执行此操作的正确方法是在您的linkedin 配置中指定回调属性。

我还在学习这个,所以我的配置中的其他东西可能是错误的。但是,回调参数解决了这个问题。

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
}
于 2012-11-13T00:50:55.170 回答