无法通过sengrid v3 api发送电子邮件(java.io.IOException:请求返回状态码401)

gstackoverflow:

我尝试通过sendGrid发送电子邮件。

我已经在网站上创建了api密钥:

在此处输入图片说明

在此处输入图片说明

并编写了以下代码:

Email fromEmail = new Email("[email protected]");
            subject = "Sending with SendGrid is Fun";
            Email toEmail = new Email("[email protected]");
            Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
            Mail mail = new Mail(fromEmail, subject, toEmail, content);

            SendGrid sg = new SendGrid("9Vh5Wz9CQryyXVhWMFFQSw");
            Request request = new Request();
            try {
                request.setMethod(Method.POST);
                request.setEndpoint("mail/send");
                request.setBody(mail.build());
                Response response = sg.api(request);
                System.out.println(response.getStatusCode());
                System.out.println(response.getBody());
                System.out.println(response.getHeaders());
            } catch (IOException ex) {
                throw ex;
            }

我得到以下异常:

java.io.IOException: Request returned status Code 401Body:{"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}
    at com.sendgrid.Client.executeApiCall(Client.java:287)
    at com.sendgrid.Client.post(Client.java:192)
    at com.sendgrid.Client.api(Client.java:310)
    at com.sendgrid.SendGrid.makeCall(SendGrid.java:151)
    at com.sendgrid.SendGrid.api(SendGrid.java:173)
    at pack.shared.email.EmailMessage$EmailService.sendEmail(EmailMessage.java:387)

我做错了什么?

niclaszll:

不要使用API​​ KEY ID。创建API密钥后,您将必须使用SendGrid显示的生成的密钥。如果您错过写下它,则需要创建一个新密钥。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章