2013-10-23 61 views
0

有一點特殊之處,嘗試創建try catch代碼片段來處理GoogleTokenResponse,並且編譯器在嘗試時拋出異常錯誤。有任何想法嗎?grails:嘗試拋出意外的令牌:嘗試:

錯誤消息:

| Loading Grails 2.2.4 
| Configuring classpath. 
| Environment set to development..... 
| Packaging Grails application.... 
| Compiling 1 source files. 
| Error Compilation error: startup failed: 
/Users/bengtbjorkberg/Documents/grails-work/iCRM/grails-app/services/icrm/GlAuthService.groovy: 44: unexpected token: try @ line 44, column 3. 
     try { 
    ^
1 error 

和有關

def setCredentila() 
    HttpTransport transport = new NetHttpTransport() 
    JacksonFactory jsonFactory = new JacksonFactory() 
    String clientId = grailsApplication.config.googleId.glAppsId 
    String clientSecret = grailsApplication.config.googleId.clientSecret 
    String redirectUrl = grailsApplication.config.googleUrl.redirectURL 

    try { 
     GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, clientId, clientSecret,session.code, redirectUrl).execute() 
    } catch (GoogleTokenResponseException e) { 
    if (e.getDetails() != mull){ 
     log.error e.getDetails().getError() 
     if (e.getDetails().getErrorDescription() != null) { 
      log.error e.getDetails().getErrorDescription() 
      } 
      if (e.getDetails().getErrorUri() != null) { 
      log.error e.getDetails().getErrorUri() 
      } 
     } else { 
      log.error e.getMessage() 
     } 
    } 


} 
GoogleCredential accessToken = new GoogleCredential.Builder().setClientSecrets(clientId, clientSecret).setJsonFactory(jsonFactory).setTransport(transport).build().setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken()); 
session.accessToken = accessToken 

}

+2

也許一個錯字,但'高清setCredentila()'應該是'高清setCredentila(){' – Grooveek

+0

這是一個錯字,但那裏有另外兩個問題,以及:)首先是它TokenResponseException不GoogleTokenResponseException ,或者至少我覺得這樣還不能令人滿意。其次是我在方括號數中輸了很多 – vrghost

+1

所以...現在可以嗎?你能更新你的代碼,並告訴現在有什麼問題嗎? – Grooveek

回答

1

因此,代碼,拖地約了一下後,我設法解決所有的問題,就是那個拋出意想不到的令牌只是我忘了把squirly括號放在正確的地方。另一個問題是我確實拼錯了setCredential,我接下來可能會注意到它。 GoogleTokenResponseException也有一個問題,應該是TokenResponseException

以下是完整的setCredential代碼來生成對谷歌的oauth2回調令牌:

def setCredential(HttpSession session) { 
    HttpTransport transport = new NetHttpTransport() 
    JacksonFactory jsonFactory = new JacksonFactory() 
    String clientId = grailsApplication.config.googleId.glAppsId 
    String clientSecret = grailsApplication.config.googleId.glClientSecret 
    String redirectUrl = grailsApplication.config.googleUrl.glRedirectUrl 
    log.info "Requesting key for client Id: " + clientId 
    log.debug "Client Secret: " + clientSecret 
    log.debug "redurectUrl: " + redirectUrl 
    try { 
     GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, clientId, clientSecret,session.code, redirectUrl).execute() 
     log.debug "Access token: " + response.getAccessToken() 
     GoogleCredential accessToken = new GoogleCredential.Builder().setClientSecrets(clientId, clientSecret).setJsonFactory(jsonFactory).setTransport(transport).build().setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken()); 
     session.accessToken = response.getAccessToken() 
     glAuth = true 

    } catch (TokenResponseException e) { 
     if (e.getDetails() != null){ 
      log.error "TokenResponseException Detials:" + e.getDetails().getError() 
      if (e.getDetails().getErrorDescription() != null) { 
       log.error "TokenResponseException descipriton" + e.getDetails().getErrorDescription() 
      } 
      if (e.getDetails().getErrorUri() != null) { 
       log.error "TokenResponseException Error Url" +e.getDetails().getErrorUri() 
      } 
     } else { 
      log.error "TokenResponseException Message" +e.getMessage() 
     } 
    } 
} 

凡在配置變量是

grailsApplication.config.googleId.glAppsId   
//Contains the Application if, format 0000000000.apps.googleusercontent.com 
grailsApplication.config.googleId.glClientSecret 
//The secret from google API console 
grailsApplication.config.googleUrl.glRedirectUrl 
//The redirection Url, not certain why it needs its, but it gets awfully upset if it does not. 

glAuth和session.accessToken可能需要外部設定的捕獲

我沒有設法弄出來的唯一的事情是在哪裏拿起瀏覽器的錯誤代碼(或ti HTTP錯誤代碼,200 OK 400 Authenticatio n錯誤)

相關問題