首页 > 解决方案 > Google API FAILED_PRECONDITION 前置条件检查失败

问题描述

我目前在调用 Gmail API 时遇到 400 错误。

我在我的 Google Cloud 控制台中启用了 Gmail API(它是 G Suite 帐户,而不是个人帐户)。

我已经创建了一个服务帐户,然后尝试使用邮递员来测试 Gmail API。

我正在使用我的服务帐户来交换承载令牌

我已经要求了所有可能的范围。

[https://www.googleapis.com/auth/gmail.addons.current.message.readonly,https://www.googleapis.com/auth/gmail.addons.current.action.compose,https://www .googleapis.com/auth/gmail.settings.basic, https://www.googleapis.com/auth/gmail.labels, https://www.googleapis.com/auth/gmail.settings.sharing, https:// /www.googleapis.com/auth/gmail.addons.current.message.metadata,https://www.googleapis.com/auth/gmail.readonly,https://www.googleapis.com/auth/gmail.send , https://www.googleapis.com/auth/gmail.addons.current.message.action, https://www.googleapis.com/auth/gmail.compose, https://www.googleapis.com/auth /gmail.insert,https://www.googleapis.com/auth/gmail.metadata,https://mail.google.com/,https://www.googleapis.com/auth/gmail.modify]

然后我使用令牌访问以下端点。

获取https://gmail.googleapis.com/gmail/v1/users/me/messages

它给了我以下错误。我已经在谷歌上搜索了几天,但我没有运气。

{
    "error": {
        "code": 400,
        "message": "Precondition check failed.",
        "errors": [
            {
                "message": "Precondition check failed.",
                "domain": "global",
                "reason": "failedPrecondition"
            }
        ],
        "status": "FAILED_PRECONDITION"
    }
}

这是我的代码:

public static void main(String[] args) {
    try {

        NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        GoogleCredentials credentials = ServiceAccountCredentials
                .fromStream(new FileInputStream("/home/rl/.gsutil/prive-production-appDev.json"));

        credentials = credentials.createScoped(GmailScopes.all());

        System.out.println(credentials.getAuthenticationType());

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage email = new MimeMessage(session);

        email.setFrom(new InternetAddress("xxxx"));
        email.addRecipient(javax.mail.Message.RecipientType.TO,
                new InternetAddress("xxxx"));
        email.setSubject("test");
        email.setText("test");

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        email.writeTo(buffer);

        String encodedEmail = Base64.encodeBase64URLSafeString(buffer.toByteArray());
        com.google.api.services.gmail.model.Message message = new com.google.api.services.gmail.model.Message();
        message.setRaw(encodedEmail);

        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

        Gmail gmail = new Gmail.Builder(httpTransport, JacksonFactory.getDefaultInstance(), requestInitializer)
                .setApplicationName("Prive Technologies")
                .build();

        ListMessagesResponse res = gmail.users().messages().list("me").execute();

        message = gmail.users().messages().send("me", message).execute();

    } catch (IOException | MessagingException | GeneralSecurityException e) {
        e.printStackTrace();
    }
}

我正在使用这个:

标签: gmail-api

解决方案


推荐阅读