grails:尝试抛出意外的令牌:尝试:

时间:2013-10-23 07:27:29

标签: java grails

有一个特殊的,尝试创建一个try catch代码来处理GoogleTokenResponse,并且编译器在try上抛出异常错误。有什么想法吗?

错误消息:

| 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

}

1 个答案:

答案 0 :(得分:1)

所以,在拖了一下之后,我设法解决了所有问题,那个抛出意外令牌的那个人就是我忘记把松鼠托架放在正确的地方。另一个问题是我确实拼错了setCredential,我很可能会注意到它。 GoogleTokenResponseException也存在问题TokenResponseException

以下是为google oauth2回调生成令牌的完整setCredential代码:

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可能需要在catch

之外设置

我唯一没有设法解决的问题是在哪里获取浏览器错误代码(或者是http http错误代码,200 OK 400身份验证错误)