英文:
Golang - Gmail API 400 BadRequest Failed Precondition
问题
我已经努力对抗这个错误(googleapi: Error 400: Bad Request, failedPrecondition
)大约8个小时了。我编写了一个简单的Go程序来重现这个问题。也许熟悉Google APIs的人可以帮助我解决。
这个错误与这里提到的错误相同:https://stackoverflow.com/questions/29327846/gmail-rest-api-400-bad-request-failed-precondition。唯一的区别是它是用Go编写的。我按照步骤进行了操作,但仍然没有运气。
我还根据https://stackoverflow.com/questions/25382821/how-to-send-email-through-gmail-go-sdk/25387678#25387678更新了我的代码。
这两个问题都有帮助,但没有帮助解决最新的问题。我还将GOOGLE_APPLICATION_CREDENTIALS
设置为我的项目的JSON。
运行上面的代码应该会产生以下错误:
2015/12/12 22:16:46 failed to send email: googleapi: Error 400: Bad Request, failedPrecondition
英文:
I've been struggling against this error (googleapi: Error 400: Bad Request, failedPrecondition
) for about 8 hours now. I've written a simple go program which reproduces the problem for me. Maybe someone familiar with the Google APIs can help me out.
The error is the same as the one mentioned here: https://stackoverflow.com/questions/29327846/gmail-rest-api-400-bad-request-failed-precondition. The only difference is that it is in Go. I've followed the steps but still no luck.
I've also updated my code based on https://stackoverflow.com/questions/25382821/how-to-send-email-through-gmail-go-sdk/25387678#25387678
Both questions were helpful but did not help resolve the latest issue. I have also set GOOGLE_APPLICATION_CREDENTIALS
to my project's json.
package main
import (
"encoding/base64"
"fmt"
"log"
"net/mail"
"strings"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
gmail "google.golang.org/api/gmail/v1"
)
func main() {
// Use oauth2.NoContext if there isn't a good context to pass in.
ctx := context.TODO()
client, err := google.DefaultClient(ctx, gmail.GmailSendScope)
if err != nil {
panic(err)
}
// create service
svc, err := gmail.New(client)
if err != nil {
log.Fatalf("Unable to create Gmail service: %v", err)
}
if err := SendEmail(ctx, svc, <redacted>); err != nil {
log.Fatalf("failed to send email: %v", err)
}
}
func SendEmail(ctx context.Context, svc *gmail.Service, email string) error {
from := mail.Address{"", "noreply@example.com"}
header := make(map[string]string)
header["From"] = from.String()
header["To"] = email
header["Subject"] = encodeRFC2047("Hello, Gmail!")
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
var msg string
for k, v := range header {
msg += fmt.Sprintf("%s: %s\r\n", k, v)
}
msg += "\r\n" + "Hello, Gmail!"
gmsg := gmail.Message{
Raw: encodeWeb64String([]byte(msg)),
}
_, err := svc.Users.Messages.Send("me", &gmsg).Do()
return err
}
func encodeRFC2047(s string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{s, ""}
return strings.Trim(addr.String(), " <>")
}
func encodeWeb64String(b []byte) string {
s := base64.URLEncoding.EncodeToString(b)
var i = len(s) - 1
for s[i] == '=' {
i--
}
return s[0 : i+1]
}
Running the code above should produce the following error:
2015/12/12 22:16:46 failed to send email: googleapi: Error 400: Bad Request, failedPrecondition
答案1
得分: 1
要发送邮件,您必须为“From:”标头指定一个有效的地址(https://support.google.com/mail/answer/22370)。考虑到example.com是一个仅供测试的域名,noreply@example.com
不太可能是一个有效的地址,根据RFC 2606。
英文:
To send mail, you must specify a valid address for the From: header (https://support.google.com/mail/answer/22370). noreply@example.com
is unlikely to be such an address given that example.com is a test-only domain per RFC 2606.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论