Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java google oauth无法获取重定向url_Java_Oauth 2.0_Jetty - Fatal编程技术网

Java google oauth无法获取重定向url

Java google oauth无法获取重定向url,java,oauth-2.0,jetty,Java,Oauth 2.0,Jetty,我正在使用googleoauth2客户端授权web应用程序(liferayportlet)使用日历服务 在我的开发服务器上,整个流程成功完成: 我开始创建一个GoogleAuthorizationCodeRequestUrl 使用com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver我创建了一个新的重定向URI,并将其设置为等待google响应 将在用户浏览器中打开一个新窗口/选项卡,指向googleAuth

我正在使用googleoauth2客户端授权web应用程序(liferayportlet)使用日历服务

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

  • 我开始创建一个GoogleAuthorizationCodeRequestUrl
  • 使用
    com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
    我创建了一个新的重定向URI,并将其设置为等待google响应
  • 将在用户浏览器中打开一个新窗口/选项卡,指向googleAuthorizationCodeRequestUrl
  • 用户登录(如果尚未登录)
  • 用户授权所请求的作用域
  • 选项卡自动关闭,jetty的回调URI从Google获取
  • 该流程继续进行令牌交换等

  • 但是,在其他远程服务器(相同的环境)上部署时,流程会卡在步骤6中。谷歌似乎无法找到重定向uri。我的浏览器登录到一个错误页面,通知他们无法在
    localhost:[从jetty生成的随机端口]
检查日志,我可以看到在这两种情况下(开发人员/远程服务器),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();

不是通过以下方式创建LocalServerReceiver的实例:

// Creating a local receiver
LocalServerReceiver receiver = new LocalServerReceiver();
您应该使用LocalServerReceiver.Builder来实现这一点

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

因此,您可以使用、设置适当的主机名(远程服务器)并构建LocalServerReceiver实例。(或者您可以使用
LocalServerReceiver(主机、端口)
constructor)


这应该设置
重定向\u uri
到正确的地址。

您的开发服务器在本地主机上吗?你能写些代码吗?e、 您是如何通过LocalServerReceiver创建重定向URI的?是的,dev服务器是localhost(它可以工作),但是远程服务器有一个dns。我将编辑并发布一些codethanx供您参考。我将尝试这个漂亮的捕获,我使用了这个现有的代码来扩展它,所以我错过了LocalServerReceiver设置。我在远程服务器的设置上遇到了一些问题,所以我无法完全测试它,但我可以看到jetty正在创建一个新的重定向url。又来了!