gpt4 book ai didi

java - grails: try 抛出意外的标记: try:

转载 作者:行者123 更新时间:2023-12-01 13:57:45 41 4
gpt4 key购买 nike

有一点特殊,尝试创建一段 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

}

最佳答案

所以,经过一番清理后,我设法解决了所有问题,抛出意外标记的只是我忘记将尖括号放在正确的位置。另一个问题是我确实拼错了 setCredential,接下来我很可能会注意到这一点。 GoogleTokenResponseException 也存在问题,它应该是 TokenResponseException

以下是为 google oauth2 回调生成 token 的完整 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错误代码,200 OK 400身份验证错误)

关于java - grails: try 抛出意外的标记: try:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535423/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com