FAILED_PRECONEDITION GMAIL API JAVA
我一直在与Google Gmail API挣扎了很长时间,我正试图使用服务帐户访问Gmail API,我的目标是使用Java Server的服务帐户发送电子邮件。 而且我不断遇到失败的问题。
- 我已经在Google Cloud平台上创建了一个项目。
- 创建的凭证
- 启用了Gmail API,
- 启用了域范围内代表团中的以下范围(必须审查完整链接):
邮件。谷歌 。 com,
/auth/gmail.compose
/auth/gmail.modify
/auth/gmail.readonly
/auth/gmail.send
我当前创建了一个Maven测试项目,试图执行一个简单的任务,以便
使用以下代码simpletask 访问gmail api ():
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()
}
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()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论