I have read this, this (PHP but to get any help), this, this and then the deprecated related changes here and the reply to the last one that points to the github where the credentials process has been explained. But am having no success with getting the sample code here to work.
The steps that I have taken :
The error "code 400, FAILED_PRECONDITION persisted.
Two specific issues :
The code is here :
public class Main {
private static final String APPLICATION_NAME = "Gmail mail server";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
public static void main(String[] args) throws IOException, MessagingException {
final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("resources/credentials.json"))
.createScoped(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_MODIFY, GmailScopes.MAIL_GOOGLE_COM);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(creds);
HttpTransport transport = new NetHttpTransport.Builder().build();
// Construct the gmail object.
Gmail gm = new Gmail.Builder(transport, JSON_FACTORY, requestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
//create the message
String to = "<a real email id>"; //actual email address used, is masked for privacy
String from = "<GCP Project Owner email ID>"; //actual email address that owns the GCP project
String subject = "Test...";
String bodytext = "\nHello .. first test email ...";
MimeMessage an_email = createEmail(to, from, subject, bodytext);
Message ret_val = sendMessage(gm, "<actual email of GCP Project Owner, not string me", an_email);
}
}
The rest of the code is an actual copy paste of what has been provided in the GCP Gmail API so am not reproducing it all over again.
All guidance is welcome. My proficiency in Java is average, so please help with a little more detailed guidance.
Thanks
GoogleCredentials
use createDelegated(String user):final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("resources/credentials.json"))
.createScoped(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_MODIFY, GmailScopes.MAIL_GOOGLE_COM);
final GoogleCredentials delegatedCreds = creds.createDelegated(userEmail);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCreds);
HttpTransport transport = new NetHttpTransport.Builder().build();
// Construct the gmail object.
Gmail gm = new Gmail.Builder(transport, JSON_FACTORY, requestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
userEmail
.