Gmail service insert MimeMessage

Viewed 82

I have Problem when calling Gmail service to insert a MimeMessage

GoogleCredential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(accountId)
                    .setServiceAccountPrivateKeyFromP12File(new File(urlFile))
                    .setServiceAccountScopes(
                            Collections.singleton(GmailScopes.GMAIL_INSERT))
                    .setServiceAccountUser(accountUser).build();

            Gmail service = new Gmail.Builder(httpTransport, JSON_FACTORY,
                    credential).build();
....

message = service.users().messages().insert(accountUser, message)
                    .setDeleted(Boolean.TRUE).execute();

I receive a 400 error :

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "invalid_grant", "error_description" : "java.security.SignatureException: Invalid signature for token:.... }

1 Answers

There appears to be something wrong with your delegation setup for one you have forgoten to add the google workspace domain user who you wish to delegate as check setServiceAccountUser.

GoogleCredential credential = new GoogleCredential.Builder()
  .setTransport(httpTransport)
  .setJsonFactory(jsonFactory)
  .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
  .setServiceAccountScopes(GmailScopes.GMAIL_INSERT)
  .setServiceAccountUser(googleWorkspaceDomainUserToDeligateTo)
  .setServiceAccountPrivateKeyFromP12File(
      new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
  .build();

That being said this is not the error that i would expect to cause an invalid_grant. I think thats being caused by the fact that you havent configured the service account properly in Google workspace.

Related