从Java读取谷歌表时,谷歌API重定向URI问题 [英] Google api redirect uri issue while reading google sheet from java

查看:70
本文介绍了从Java读取谷歌表时,谷歌API重定向URI问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用oauth2 Google驱动器API从Java中的Google表格中读取数据.我在本地计算机上有一个工作示例.但是,当我将其部署在远程服务器上时,在进行身份验证时,API返回一个URL,该URL将在浏览器中打开.网址如下:

I am using oauth2 google drive API to read data from a google sheet in Java. I have an working sample on my local machine. But when I deploy it on the remote server, at the time of authentication the API returns a URL which is to be opened in browser. The URL is like below :

https://accounts.google.com/o/oauth2/auth?access_type=online&approval_prompt=auto&client_id=my.client.id&redirect_uri=http://**localhost:58305**/Callback&response_type=code&scope=https://www.googleapis.com/auth/drive

显然,由于没有本地主机,因此该URL将不会在远程服务器上打开.

Obviously this URL will not open on remote server as there is no localhost.

我的Google开发人员控制台配置,同时生成oauth客户端ID:

My google developers console configuration while generating oauth client id:

  1. 应用程序类型:Web应用程序或其他

  1. Application type : Web Application or Other

授权的JavaScript来源:我的远程服务器来源

Authorized JavaScript origins : My remote server origin

授权的重定向URI:我的远程服务器页面

Authorized redirect URIs : My remote server page

当应用程序"类型为"其他"时,身份验证已在我的本地计算机上成功完成.部署在远程服务器上时,由于身份验证URL中的 localhost 字符串而导致身份验证失败.

The authentication gets successfully done on my local machine when the Application type is Other. The same when deployed on remote server fails to authenticate because of the localhost string inside the authentication URL.

另一方面,当应用程序类型为 Web应用程序时,即使我在上述选项中添加了正确的来源,它也会在localhost和远程服务器上给我一个错误页面,该页面具有如下所示的错误:

On the other hand when the application type is Web Application it gives me an error page on both localhost and remote server which has error like below even when I have added proper origins in the above options:

Error: redirect_uri_mismatch

The redirect URI in the request, http://localhost:59363/Callback, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/my.client.id?project=my.project.id to update the authorized redirect URIs.

我尝试了重定向URI的多个选项.但这会带来同样的错误.

I tried multiple options with the redirect URIs. But it gives same error.

  1. 身份验证中是否有任何设置可以更改修复 localhost:59363 部分?
  2. 应用程序类型:Web应用程序是否正确?将其作为应用程序类型:其他部署在生产服务器上已安装的应用?
  1. Is there any setting in the authentication where we can change the fix the localhost:59363 part ?
  2. Is application type : web application correct when I have to deploy this on production server as application type : other is for installed apps ?

推荐答案

您需要将重定向uri设置为与Java代码中google控制台中提供的重定向uri相同.

You need to set the redirect uri same as the one which you have provided in google console in your java code.

看看 https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#web_server_applications

    @Override
  protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
    GenericUrl url = new GenericUrl(req.getRequestURL().toString());
    url.setRawPath("/oauth2callback");
    return url.build();
  }

您构建重定向网址.当前,您将其作为localhost:58305/Callback

You build your redirect url. Currently you are sending that as localhost:58305/Callback

更新它,它应该可以工作

update it and it should work

这篇关于从Java读取谷歌表时,谷歌API重定向URI问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆