FAILED_PRECONEDITION GMAIL API JAVA

发布于 01-19 08:08 字数 2057 浏览 2 评论 0原文

我一直在与Google Gmail API挣扎了很长时间,我正试图使用​​服务帐户访问Gmail API,我的目标是使用Java Server的服务帐户发送电子邮件。 而且我不断遇到失败的问题。

  1. 我已经在Google Cloud平台上创建了一个项目。
  2. 创建的凭证
  3. 启用了Gmail API,
  4. 启用了域范围内代表团中的以下范围(必须审查完整链接):

邮件。谷歌 。 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.

  1. I have created a project in the google cloud platform.
  2. created credentials
  3. enabled gmail apis
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文