java - google oauth 获取重定向 url 失败

标签 java oauth-2.0 jetty

我正在使用 Google OAuth2 客户端授权网络应用程序 (Liferay Portlet) 使用日历服务。

  • 在我的开发服务器上,整个流程成功完成:

    1. 我开始创建 GoogleAuthorizationCodeRequestUrl
    2. 使用 com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver 我创建了一个新的重定向 URI 并将其设置为等待 Google 响应。
    3. 在用户浏览器中打开一个新窗口/标签页,指向 googleAuthorizationCodeRequestUrl
    4. 用户登录(如果尚未登录)
    5. 用户授权请求的范围
    6. 选项卡自动关闭, jetty 的回调 URI 是从 Google 获取的,
    7. 流程继续进行 token 交换等

  • 但是当在其他远程服务器(相同环境)上部署时,流程会卡在第 6 步。 Google 似乎无法找到 redirect_uri。我的浏览器登陆错误页面,通知他们无法与位于 localhost:[random port generated from jetty]
  • 的服务器建立连接

查看日志,我可以看到在这两种情况下(开发/远程服务器),jetty 创建的 redirect_uri 都是 localhost:[5digits]/Callback。 (不受远程服务器上的 dns 或 ip 的影响)这正常吗?我是否错过了配置中的某些内容?也许 jetty 应该创建另一个重定向 URI,我应该另外从 Google Dev Console 添加(显然我不能设置为 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 的实例:

// Creating a local receiver
LocalServerReceiver receiver = new LocalServerReceiver();

您应该使用 LocalServerReceiver.Builder 来完成此操作。

根据 documentation非参数构造函数是:在“localhost”上启动服务器的构造函数选择一个未使用的端口。

所以你可以使用builder ,设置正确的主机名(远程服务器)并构建 LocalServerReceiver 实例。 (或者您可以使用 LocalServerReceiver(host, port) 构造函数)

这应该将 redirect_uri 设置为正确的地址。

关于java - google oauth 获取重定向 url 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22763934/

相关文章:

java - 使用 PDFBox 设置字符间距

JavaFX 日期选择器不更新值

coldfusion - 谷歌 oauth token 给出 405 错误

java - 嵌入 Jetty 9 - jetty-all.jar 在哪里?

asynchronous - 使用 jetty 还是净值?

Java:无法打印 ArrayList 中的元素

java - Java 中的 PrepareStatement JDBC

php - Google OAuth 自动登录

windows - Google 登录 oauth 集成在 Windows PWA 应用程序中不起作用

jetty - Spark 的 Jetty 自动重载