Java google oauth无法获取重定向url
我正在使用googleoauth2客户端授权web应用程序(liferayportlet)使用日历服务Java google oauth无法获取重定向url,java,oauth-2.0,jetty,Java,Oauth 2.0,Jetty,我正在使用googleoauth2客户端授权web应用程序(liferayportlet)使用日历服务 在我的开发服务器上,整个流程成功完成: 我开始创建一个GoogleAuthorizationCodeRequestUrl 使用com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver我创建了一个新的重定向URI,并将其设置为等待google响应 将在用户浏览器中打开一个新窗口/选项卡,指向googleAuth
- 在我的开发服务器上,整个流程成功完成:
- 我开始创建一个GoogleAuthorizationCodeRequestUrl
- 使用
我创建了一个新的重定向URI,并将其设置为等待google响应com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
- 将在用户浏览器中打开一个新窗口/选项卡,指向googleAuthorizationCodeRequestUrl
- 用户登录(如果尚未登录)
- 用户授权所请求的作用域
- 选项卡自动关闭,jetty的回调URI从Google获取
- 该流程继续进行令牌交换等
- 但是,在其他远程服务器(相同的环境)上部署时,流程会卡在步骤6中。谷歌似乎无法找到重定向uri。我的浏览器登录到一个错误页面,通知他们无法在
localhost:[从jetty生成的随机端口]
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, secrets, scopes)
.setDataStoreFactory(dataStore).build();
Credential credents = flow.loadCredential(usermail);
String redirect_url = null;
// Checking if the given user is not authorized
if (credents == null) {
// Creating a local receiver
LocalServerReceiver receiver = new LocalServerReceiver();
try {
// Getting the redirect URI
String redirectUri = receiver.getRedirectUri();
// Creating a new authorization URL
AuthorizationCodeRequestUrl authorizationUrl = flow.newAuthorizationUrl();
// Setting the redirect URI
authorizationUrl.setRedirectUri(redirectUri);
// Building the authorization URL
String url = authorizationUrl.build();
// Logging a short message
log.info("Creating the authorization URL : " + url);
//This url will be fetched right after, as a button callback (target:_blank)
//by using :FacesContext.getCurrentInstance().getExternalContext().redirect(googleAuthUrl);
googleAuthUrl = url;
// Receiving authorization code
String code = receiver.waitForCode();
// Exchanging it for an access token
TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
// Storing the credentials for later access
credents = flow.createAndStoreCredential(response, id);
} finally {
// Releasing resources
receiver.stop();
}
}
// Setting up the calendar service client
client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, jsonFactory, credents).setApplicationName(APPLICATION_NAME)
.build();
不是通过以下方式创建LocalServerReceiver的实例:
// Creating a local receiver
LocalServerReceiver receiver = new LocalServerReceiver();
您应该使用LocalServerReceiver.Builder来实现这一点
根据非参数构造函数是:在“localhost”上启动服务器的构造函数选择一个未使用的端口
因此,您可以使用、设置适当的主机名(远程服务器)并构建LocalServerReceiver实例。(或者您可以使用LocalServerReceiver(主机、端口)
constructor)
这应该设置
重定向\u uri
到正确的地址。您的开发服务器在本地主机上吗?你能写些代码吗?e、 您是如何通过LocalServerReceiver创建重定向URI的?是的,dev服务器是localhost(它可以工作),但是远程服务器有一个dns。我将编辑并发布一些codethanx供您参考。我将尝试这个漂亮的捕获,我使用了这个现有的代码来扩展它,所以我错过了LocalServerReceiver设置。我在远程服务器的设置上遇到了一些问题,所以我无法完全测试它,但我可以看到jetty正在创建一个新的重定向url。又来了!