79598878

Date: 2025-04-29 17:20:03
Score: 0.5
Natty:
Report link

I stumbled across a couple of posts FAILED_PRECONDITION - Gmail API which provided an answer that worked for my case:

This line from the google code sample:

credentials = ServiceAccountCredentials.fromStream(stream).createScoped(GmailScopes.GMAIL_SEND);

needs to change to this:

ServiceAccountCredentials.fromStream(stream).createScoped(GmailScopes.GMAIL_SEND).createDelegated(workspaceEmailAddress);

The .createDelegate(workspaceEmailAddress) apparently ensures that the impersonation is happening at the right place.

Also for a bonus, to send an HTML formatted email, we need to change this line from the google sample code:

email.setText(htmlBodyText);

to this:

email.setContent(htmlBodyText, "text/html");
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: jn4