0

I am trying to send email using gmail api service account from spring boot api.

GoogleCredentials credentials = ServiceAccountCredentials.fromStream(inputStream)
                .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
        credentials = credentials.createScoped(SCOPES);
        credentials.refresh();
        AccessToken refreshToken = credentials.refreshAccessToken();
        OAuth2Credentials credentialsAccess = OAuth2Credentials.create(refreshToken);
        LOGGER.info("refresh token {}", credentials.refreshAccessToken().getTokenValue());
        LOGGER.info("refresh token {}", credentialsAccess.getAccessToken().getTokenValue());
        
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
        
        Gmail service = new Gmail.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(),
                requestInitializer).setApplicationName(APPLICATION_NAME).build();
        Users a = service.users();
        sendMessage(service, "me", mimeMessage);

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    Did you set up domain wide delegation to the service account from your gsuite domain? is the email you are trying to connect to delegate to actually on your gsuite domain or is it a normal gmail.com email? – Linda Lawton - DaImTo Feb 03 '21 at 10:17
  • Does this answer your question? [Gmail REST API : 400 Bad Request + Failed Precondition](https://stackoverflow.com/questions/29327846/gmail-rest-api-400-bad-request-failed-precondition) – fullfine Feb 03 '21 at 14:55

0 Answers0