getResourceAsStream 获取客户端密钥

getResourceAsStream fetch clientsecret

提问人:Arif Pepaj 提问时间:7/11/2023 最后编辑:RobertArif Pepaj 更新时间:7/12/2023 访问量:21

问:

我有这个我使用的userEndpoint(在wildfly 27和jakarta ee中),当我尝试在Postman中运行时,它给出了一个错误,但是当我尝试使用该方法时,它起作用了。我尝试使用 API 调用发送电子邮件。main

这是 userservice:

public void sendConfirmAccountEmail(String email, String token) throws Exception {
    GMailer emailService = new GMailer();
    emailService.sendMail(email, "Confirm Account",
            "Click the following link to confirm your account: http://localhost:8080/user/register/confirm?"
                    + token);
}

和这个端点

@GET
@Path("/send/{token}")
public Response email(@PathParam("token") String token, String email) {
    try {
        GMailer emailService = new GMailer();
        emailService.sendMail(email, "Confirm Account",
                "Click the following link to confirm your account: http://localhost:8080/user/users/confirm/"
                        + token);

    } catch (Exception e) {
        System.out.println("Gabim ne userEndpoint");
        e.printStackTrace();
    }
    return Response.status(201).build();
}

这是类:

public class GMailer {
    private Gmail service;

    public GMailer() throws Exception {
        NetHttpTransport httpTransport = createHttpTransport();
        GsonFactory jsonFactory = GsonFactory.getDefaultInstance();
        service = new Gmail.Builder(httpTransport, jsonFactory, getCredentials(httpTransport, jsonFactory))
                .setApplicationName("Test Email").build();
    }

    private NetHttpTransport createHttpTransport() throws Exception {
        NetHttpTransport httpTransport = new NetHttpTransport.Builder().build();
        return (NetHttpTransport) httpTransport;
    }

    public Credential getCredentials(final NetHttpTransport httpTransport, GsonFactory jsonFactory) throws IOException {
        GoogleClientSecrets clientSecrets = loadClientSecrets(jsonFactory);
        GoogleAuthorizationCodeFlow flow = createAuthorizationCodeFlow(httpTransport, jsonFactory, clientSecrets);
        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
        return credential;
    }

    private GoogleClientSecrets loadClientSecrets(GsonFactory jsonFactory) throws IOException {
        InputStream in = GMailer.class.getResourceAsStream("/client_secret");
        if (in == null) {
            throw new FileNotFoundException("Resource file not found");
        }
        return GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));
    }

    private GoogleAuthorizationCodeFlow createAuthorizationCodeFlow(NetHttpTransport httpTransport,
            GsonFactory jsonFactory, GoogleClientSecrets clientSecrets) throws IOException {
        return new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets,
                Set.of(GmailScopes.GMAIL_SEND))
                .setDataStoreFactory(new FileDataStoreFactory(Paths.get("tokens").toFile())).setAccessType("offline")
                .build();
    }

    public void sendMail(String useremail, String subject, String text) throws Exception {
        MimeMessage email = createEmail(useremail, subject, text);
        Message message = createMessage(email);
        message = service.users().messages().send("me", message).execute();
        System.out.println("Message id: " + message.getId());
        System.out.println(message.toPrettyString());
    }

    private MimeMessage createEmail(String useremail, String subject, String text) throws Exception {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage email = new MimeMessage(session);
        email.setFrom(new InternetAddress(useremail));
        email.addRecipient(jakarta.mail.Message.RecipientType.TO, new InternetAddress(useremail));
        email.setSubject(subject);
        email.setText(text);
        return email;
    }

    private Message createMessage(MimeMessage email) throws Exception {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        email.writeTo(buffer);
        byte[] rawMessageBytes = buffer.toByteArray();
        String encodedEmail = Base64.encodeBase64URLSafeString(rawMessageBytes);
        Message message = new Message();
        message.setRaw(encodedEmail);
        return message;
    }

为什么会这样,我该如何解决?

java 电子邮件 gmail jakarta-mail

评论


答: 暂无答案