【发布时间】:2014-03-31 14:14:26
【问题描述】:
我正在使用 Google OAuth2 客户端授权网络应用程序 (Liferay Portlet) 使用日历服务。
-
在我的开发服务器上,整个流程成功完成:
- 我开始创建 GoogleAuthorizationCodeRequestUrl
- 我使用
com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver创建了一个新的重定向 URI 并将其设置为等待 Google 响应。 - 在用户浏览器中打开一个新窗口/选项卡,用于 googleAuthorizationCodeRequestUrl
- 用户登录(如果尚未登录)
- 用户授权请求的范围
- 标签页自动关闭,码头的回调 URI 是从谷歌获取的,
- 流程继续进行令牌交换等
- 但在其他远程服务器(相同环境)上部署时,流程会卡在第 6 步。谷歌似乎无法找到 redirect_uri。我的浏览器登陆一个错误页面,通知他们无法与
localhost:[random port generated from jetty]的服务器建立连接
查看日志,我可以看到在这两种情况下(dev/remote server),jetty创建的redirect_uri都是localhost:[5digits]/Callback。 (不受远程服务器上的 dns 或 ip 影响)这是正常的吗?我错过了配置上的某些内容吗?也许码头应该创建另一个重定向 URI,我应该另外从 Google 开发控制台添加(显然我不能设置为 localhost..)
是否有可能是防火墙或代理设置阻止了 redirect_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();
【问题讨论】:
-
您的开发服务器在本地主机上吗?你能放一些代码吗?例如您如何通过 LocalServerReceiver 创建重定向 URI?
-
是的,开发服务器是本地主机(它可以工作),但是远程服务器有一个 dns。我将编辑并发布一些代码