1 year ago
#378953
Udi Kantzuker
FAILED_PRECONDITION Gmail API JAVA
I have been struggling with the google Gmail api for a long time now, I am trying to gain access to the Gmail API using service account, my goal is to send emails using service account from a java server. and I keep getting FAILED_PRECONDITION errors.
- I have created a project in the google cloud platform.
- created credentials
- enabled gmail apis
- enabled the following scopes in the domain-wide delegations (had to censor the full links):
mail. google . com,
/auth/gmail.compose
/auth/gmail.modify
/auth/gmail.readonly
/auth/gmail.send
Currently i created a maven test project trying to do a simple task just to access the gmail API
using the following code simpletask() :
private Gmail createGmail() {
GoogleCredentials googleCredentials = null;
try {
googleCredentials = getGoogleCredentials();
} catch (IOException ex) {
Logger.getLogger(GmailServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);
return new Gmail.Builder(httpTransport, JSON_FACTORY, requestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
}
public static GoogleCredentials getGoogleCredentials() throws IOException {
return GoogleCredentials.fromStream(new FileInputStream(Thread.currentThread().getContextClassLoader().getResource(auth).getFile()))
.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform","https://mail.google.com/", "https://www.googleapis.com/auth/gmail.compose", "https://www.googleapis.com/auth/gmail.modify", "https://www.googleapis.com/auth/gmail.readonly","https://www.googleapis.com/auth/gmail.send"));
}
public void simpletask(String recipientAddress, String subject, String body) throws MessagingException,
IOException {
Message message = createMessageWithEmail(
createEmail(recipientAddress, gmailCredentials.getUserEmail(), subject, body));
createGmail()
.users().messages()
.list("me")
.execute()
}
java
google-cloud-platform
gmail-api
0 Answers
Your Answer