【问题标题】:google oauth fails fetching redirect urlgoogle oauth 无法获取重定向 url
【发布时间】:2014-03-31 14:14:26
【问题描述】:

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

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

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

  • 但在其他远程服务器(相同环境)上部署时,流程会卡在第 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。我将编辑并发布一些代码

标签: java oauth-2.0 jetty


【解决方案1】:

不是这样创建 LocalServerReceiver 的实例:

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

您应该使用 LocalServerReceiver.Builder 来执行此操作。

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

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

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

【讨论】:

  • 感谢您的宝贵时间。我试试这个
  • 不错,我用这个现有的代码来扩展它,所以我错过了 LocalServerReceiver 设置。我在远程服务器上遇到了一些设置问题,所以我无法完全测试它,但我可以看到码头现在正在创建一个新的 redirect_url。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多