Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

gmail api - Google API FAILED_PRECONDITION Precondition check failed

I'm currently facing a 400 error when calling Gmail API.

I have enabled Gmail API in my Google Cloud console (it is G Suite account, not personal account).

And i have created a service account, then try to use postman to test out the Gmail API.

I'm using my service account to exchange a Bearer Token

I have requested all the possible scopes.

[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]

And then I use the token to access the following endpoint.

GET https://gmail.googleapis.com/gmail/v1/users/me/messages

It gives me the following error. I have been googling for a few days but i have no luck.

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

This is the my code:

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();
    }
}

I'm using this :


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...