【发布时间】:2018-03-15 12:46:42
【问题描述】:
我将 Gmail API 与 curl 一起使用。( Users.messages: send)
但我收到错误 400 需要收件人地址。
命令
curl -X POST -H "Authorization: Bearer *****" -H "Content-Type:message/rfc822" -d "{'raw':'Encoded Value'}" "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send"
回应
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "Recipient address required"
}
],
"code": 400,
"message": "Recipient address required"
}
}
编码值由以下 python 脚本创建。
import base64
from email.mime.text import MIMEText
from email.utils import formatdate
MAIL_FROM = "example@gmail.com"
MAIL_TO = "example@gmail.com"
def create_message():
message = MIMEText("Gmail body: Hello world!")
message["from"] = MAIL_FROM
message["to"] = MAIL_TO
message["subject"] = "gmail api test"
message["Date"] = formatdate(localtime=True)
byte_msg = message.as_string().encode(encoding="UTF-8")
byte_msg_b64encoded = base64.urlsafe_b64encode(byte_msg)
str_msg_b64encoded = byte_msg_b64encoded.decode(encoding="UTF-8")
return {"raw": str_msg_b64encoded}
print(create_message())
【问题讨论】: