2012-09-15 90 views
3

我正在嘗試爲Grails使用新的oAuth 2.0插件來消耗LinkedIn資源。使用我的代碼,我可以訪問LinkedIn授權頁面,在該頁面中,我可以授予我的應用訪問我的LinkedIn帳戶信息的權限。Grails grails-oauth-scribe linkedin重定向

問題是,一旦我點擊繼續按鈕,它不會重定向回我的應用程序。相反,它進入一個頁面,上面寫着「您已成功授權XXXX,請返回到您的應用程序並輸入以下安全代碼以授予訪問權限:某些號碼」

如何將此重定向回我的應用程序?

我的設置是:

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" 
      } 
     } 
    } 

我GSP觀點:

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

我的開發人員帳戶鏈接:

網址:http://localhost:8080/myApp OAuth的重定向網址:http://localhost:8080/myApp/secure/linkedinProfile/success

回答

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是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) 

} 
0

正確的方式來做這個與插件是指定一個回調屬性在你的配置。

我還在學習這個,所以在我的配置中的其他東西可能是錯誤的。但是,回調參數解決了這個問題。

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 
} 
+0

具體而言,我不認爲我有successUri和failureUri屬性設置正確...我是新來的,但不管它通過設置回調屬性解決了您遇到的問題。 – JSager