使用 2.0.0 google-api-client 进行 Gmail 调用时出现兼容性问题

问题描述 投票:0回答:3

我一直在开发一个小项目,该项目连接到用户的 Gmail 收件箱并使用

google-api-client
2.0.0 和
google-api-services-gmail
版本 v1-rev20220404-2.0.0

读取邮件

当我尝试构建 Gmail 服务时

service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY,
          authorize).setApplicationName(Main.APPLICATION_NAME).build();

它抛出一个 IllegalStateException ,上面写着

“您当前运行的是 2.0.0 版的 google-api-client。您至少需要 1.15 版的 google-api-client 才能运行 1.25.0 版的 Gmail API 库。”

一开始我以为可能是我安装的模块不是最新的什么的,但这并没有什么意义,所以我尝试调试并进入了Gmail.java类。

代码以非常简单的方式检查版本,如果条件为假,则抛出异常

static {
        Preconditions.checkState(GoogleUtils.MAJOR_VERSION == 1 && GoogleUtils.MINOR_VERSION >= 15,
        "You are currently running with version %s of google-api-client. You need at least version 1.15 of google-api-client to run version 1.25.0 of the Gmail API library.",
        new Object[]{GoogleUtils.VERSION});
    }

我认为这就是问题所在,我的 MAJOR_VERSION 为 2 且 MINOR_VERSION 为 0 使得该语句错误,即使我使用的版本是最新的。我不知道是否可以通过将 API 版本降级到 1.XX 来解决这个问题,无论如何我都会尝试,但是你知道我是否在这里做了什么吗?

java google-api gmail gmail-api google-api-java-client
3个回答
1
投票

解决方案是升级到v1-rev20230206-2.0.0 这是撰写本文时的最新版本

<dependency>
   <groupId>com.google.apis</groupId>
   <artifactId>google-api-services-gmail</artifactId>
   <version>v1-rev20230206-2.0.0</version>
</dependency>

Google 文档建议搜索最新版本这里


0
投票

如果这对其他人有帮助,我在升级 Firebase 管理插件时遇到了此错误。它阻止了我的 App Engine 应用程序启动。

对我来说一个简单的修复方法是恢复我的 POM 文件。

修复:将 9.1.1 降级至 8.1.0:

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>9.1.1</version>
</dependency>

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>8.1.0</version>
</dependency>

0
投票

除了@Dave B 所说的。

如果您访问 Google 的文档站点,它将显示客户端库的版本。您还可以在下面找到 Gmail 库的链接。 我会避免使用第三方网站来获取包,因为它可能不包含正确的信息,但实际上您可能正在寻找该库的版本 2。请记住提防假库,并在应用程序中使用库之前始终检查库中是否存在漏洞。 :)

https://googleapis.dev/java/google-api-services-gmail/latest/

https://cloud.google.com/java/docs/reference/google-api-client/latest/overview

© www.soinside.com 2019 - 2024. All rights reserved.