Gmail API配置问题(使用Java)

use*_*106 13 java configuration gmail spring gmail-api

这是我的Gmail服务配置/工厂类:

import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;

public class GmailServiceFactoryBean {

    private @Autowired Environment env;

    private final NetHttpTransport transport;
    private final JacksonFactory jacksonFactory;

    public GmailServiceFactoryBean() throws GeneralSecurityException, IOException {
        this.transport = GoogleNetHttpTransport.newTrustedTransport();
        this.jacksonFactory = JacksonFactory.getDefaultInstance();
    }

    public Gmail getGmailService() throws IOException, GeneralSecurityException {
        return new Gmail.Builder(transport, jacksonFactory, getCredential())
                .setApplicationName(env.getProperty("gmail.api.application.name")).build();
    }

    private HttpRequestInitializer getCredential() throws IOException, GeneralSecurityException {
        File p12File = new File(this.getClass().getClassLoader().getResource("google-key.p12").getFile());

        Credential credential = new GoogleCredential.Builder()
            .setServiceAccountId(env.getProperty("gmail.api.service.account.email"))
            .setServiceAccountPrivateKeyId(env.getProperty("gmail.api.private.key.id"))
            .setServiceAccountPrivateKeyFromP12File(p12File)
            .setTransport(transport)
            .setJsonFactory(jacksonFactory)
            .setServiceAccountScopes(GmailScopes.all())
            //.setServiceAccountUser(env.getProperty("gmail.api.user.email"))
            .build();

        credential.refreshToken();

        return credential;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的内部邮件服务,使用以前的bean:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Message;
import com.example.factory.GmailServiceFactoryBean;
import com.example.service.MailService;
import com.example.service.exception.MailServiceException;

@Service
public class MailServiceImpl implements MailService {

    private @Autowired GmailServiceFactoryBean gmailServiceFactoryBean;
    private @Autowired Environment env;

    @Override
    public void send(com.example.model.Message message, String recipient) throws MailServiceException {
        try {
            Gmail gmailService = gmailServiceFactoryBean.getGmailService();
            MimeMessage mimeMessage = createMimeMessage(message, recipient);
            Message gMessage = createMessageWithEmail(mimeMessage);
            gmailService.users().messages().send("me", gMessage).execute();
        } catch(MessagingException | IOException | GeneralSecurityException e) {
            throw new MailServiceException(e.getMessage(), e.getCause());
        }
    }

    @Override
    public void send(com.example.model.Message message, List<String> recipients) throws MailServiceException {
        for (String recipient : recipients) {
            send(message, recipient);
        }
    }

    private MimeMessage createMimeMessage(com.example.model.Message message, String recipient) throws MessagingException {
        Session session = Session.getDefaultInstance(new Properties());

        MimeMessage email = new MimeMessage(session);
        InternetAddress toAddress = new InternetAddress(recipient);
        InternetAddress fromAddress = new InternetAddress(env.getProperty("gmail.api.service.account.email"));

        email.setFrom(fromAddress);
        email.addRecipient(RecipientType.TO, toAddress);
        email.setSubject(message.getTitle());
        email.setText(message.getContent(), env.getProperty("application.encoding"));

        return email;
    }

    private Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        email.writeTo(baos);
        return new Message().setRaw(Base64.encodeBase64URLSafeString(baos.toByteArray()));
    }
}
Run Code Online (Sandbox Code Playgroud)

当我执行send(Message message, String recipient)类的方法时MailServiceImpl得到以下响应:

400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Bad Request",
    "reason" : "failedPrecondition"
  } ],
  "message" : "Bad Request"
}
Run Code Online (Sandbox Code Playgroud)

有谁知道什么是错的?

akg*_*aur 5

要使 GMail API 正常工作,您必须在 Google Apps 帐户中“将域范围的权限委托给服务帐户”。

服务帐户并不代表人工 Google 帐户。您也不能将权限委托给整个 Google 域 (***@gmail.com)。

另一种出路可能是使用适用于 Web 服务器应用程序的 OAuth 2.0Java Mail api

有关更多信息,请检查:GMail REST API:使用 Google 凭据而不进行模拟