我使用 Google OAuth2 客户端授权网络应用(Liferay Portlet)使用日历服务。
在我的开发服务器上,整个流程成功完成:
com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
我创建了一个新的重定向URI,并将其设置为等待Google Response。localhost:[random port generated from jetty]
检查日志,我可以看到在两种情况下(dev / remote server),jetty创建的redirect_uri是 localhost :[5digits] / Callback。 (不受远程服务器上的DNS或IP的影响)这是正常的吗?我错过了配置上的一些内容吗?也许jetty应该创建另一个重定向URI,我还应该从Google Dev Console添加(显然我不能设置为localhost ..)
防火墙或代理设置是否可能阻止redirect_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();
答案 0 :(得分:1)
而不是通过这个创建LocalServerReceiver的实例:
// Creating a local receiver
LocalServerReceiver receiver = new LocalServerReceiver();
您应该使用LocalServerReceiver.Builder执行此操作。
根据documentation非参数构造函数是:在“localhost”上启动服务器的构造函数选择一个未使用的端口。
因此,您可以使用builder,设置正确的主机名(远程服务器)并构建LocalServerReceiver实例。 (或者您可以使用LocalServerReceiver(host, port)
构造函数)
这应该将redirect_uri
设置为正确的地址。