java google oauth无法获取重定向url
我正在使用Google OAuth2客户端授权web应用程序(Liferay Portlet)使用日历服务
在我的开发服务器上,整个流程成功完成:
- 我开始创建一个GoogleAuthorizationCodeRequestUrl
- 我使用
com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
创建了一个新的重定向URI,并将其设置为等待Google响应李> - 在用户浏览器中打开一个新窗口/选项卡,指向googleAuthorizationCodeRequestUrl
- 用户登录(如果尚未登录)
- 用户授权所请求的作用域
- 选项卡自动关闭,jetty的回调URI从Google获取
- 流程将继续进行令牌交换等
- 但是当在其他远程服务器(相同的环境)上部署时,流程会卡在步骤6中。谷歌似乎无法找到重定向uri。我的浏览器登录到一个错误页面,通知他们无法在
localhost:[random port generated from jetty]
建立与服务器的连接
通过查看日志,我可以看到,在这两种情况下(dev/remote server),jetty创建的重定向uri都是localhost:[5digits]/Callback。(不受远程服务器上的dns或ip影响)这正常吗?我是不是错过了一些配置?也许jetty应该创建另一个重定向URI,我应该从Google开发控制台额外添加它(显然我不能设置为localhost…)
是否可能是防火墙或代理设置阻止了重定向url
还有其他想法吗?我做错了什么
编辑:发布一些URL创建代码
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();
# 1 楼答案
而不是通过以下方式创建LocalServerReceiver的实例:
您应该使用LocalServerReceiver来实现这一点。建筑商
根据documentation,非参数构造函数是:在“localhost”上启动服务器的构造函数选择一个未使用的端口
因此,您可以使用builder,设置适当的主机名(远程服务器)并构建LocalServerReceiver实例。(也可以使用
LocalServerReceiver(host, port)
构造函数)这应该将
redirect_uri
设置为正确的地址