java - 在heroku上部署spring boot时Google api重定向问题

标签 java spring-boot heroku google-sheets-api google-oauth

我正在使用 Spring Boot 制作一个应用程序。它从 Google Sheet API 获取数据。我按照本教程进行操作,它在我的本地计算机上运行良好。 教程:https://www.youtube.com/watch?v=8yJrQk9ShPg&t=172s

在 Heroku 上部署应用程序后,我遇到了与这篇文章相同的问题 Google api redirect uri issue while reading google sheet from java

我尝试跟随

  1. https://{my-app}.herokuapp.com/ 添加到授权域

  2. 使用Web应用程序类型创建Google API凭据,并设置授权将URI重定向到我的Heroku域https://{my-app}.herokuapp.com/

  3. 下载它并替换在本地运行时有效的版本。

    {
      "web": {
        "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
        "project_id": "xxxxxxxxxxxxxxx",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_secret": "xxxxxxxxxxxxxxxxxx",
        "redirect_uris": [
          "https://{my-app}.herokuapp.com/"
        ]
      }
    }
  • 创建新类OAuth2Callback 扩展 AbstractAppEngineAuthorizationCodeCallbackServlet
  •     @Override
        protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
            GenericUrl url = new GenericUrl("https://{my-app}.herokuapp.com");
            url.setRawPath("/Callback");
            return url.build();
        }
    
  • 部署到 Heroku
  • 完成这些步骤后,第一次运行时登录 Heroku 显示与 localhost 相同

    Please open the following address in your browser:
    https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=http://localhost:45200/Callback&response_type=code&scope=https://www.googleapis.com/auth/spreadsheets.readonly
    

    我复制并在浏览器中打开它。然后它会抛出错误:redirect_uri_mismatch,因为redirect_uri仍然是localhost。 我预计应该将redirect_uri更改为https://{my-app}.herokuapp.com,但没有。 我还尝试在提供的 URL 中手动将 localhost:42500 更改为 https://{my-app}.herokuapp.com,但它仍然引发相同的错误。

    请帮助我更正我的身份验证配置。谢谢。

    • 这是我的第一篇文章,抱歉我的语法不好。

    最佳答案

    我找到了解决方案。 实际上,在我的应用程序中,我应该实现“将 OAuth 2.0 用于服务器到服务器应用程序”,它使用服务帐户的凭据,而不是“将 OAuth 2.0 用于 Web 服务器应用程序” 链接:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

    这是步骤

    1. 从同一项目创建 Google 服务帐户 https://console.developers.google.com/然后下载并放置在资源文件夹
    2. 更改使用的获取凭据方法 credential = AuthorizationCodeInstalledApp(GoogleAuthorizationCodeFlow, VerificationCodeReceiver) (示例 https://developers.google.com/api-client-library/java/ ) 到 GoogleCredential,请参阅下文
        public static Credential getCredential() throws IOException {
            if (credential == null) {
                InputStream is = GoogleSheetsCredential.class
                  .getResourceAsStream("/sheets-service-account.json");
                credential = GoogleCredential.fromStream(is)
                  .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
            }
            return credential;
        }
    

    然后我部署到 Heroku 并且它有效! 如果有人与我遇到同样的问题,希望这会有所帮助。

    这里是教程引用:https://techxlore.com/google-sheets-api-service-account/

    关于java - 在heroku上部署spring boot时Google api重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59588881/

    相关文章:

    java - 将多个数组列表保存并检索到文件中

    Java - JAXB - 解码 : How to achieve the correct set up?

    spring-boot - 在 neo4j 中创建一个具有除 ID 之外的唯一属性的节点

    ruby-on-rails - 这些 Ruby 版本中存在 heroku 命令吗?

    java - 如何在java中调整sin(x)图形坐标以仅填充窗口的一部分

    java - 从不同语言调用 Web 服务 (asmx)

    json - gradle spring boot web服务返回json “java.lang.IllegalArgumentException ”没有转换

    java - 在没有主应用程序的情况下从 application.properties 获取值(value)

    Node.js + Socket.io 应用程序 - 在heroku 中不起作用

    heroku - 在推送到 heroku 时无法读取未定义的属性 'match'