我使用了卷曲的Gmail API。( 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())
答案 0 :(得分:1)
当使用https://www.googleapis.com/upload/gmail/v1/users/me/messages/send
通过媒体上传请求发送消息时,需要按如下方式创建请求正文。我修改了你的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 encode(v):
byte_msg = v.encode(encoding="UTF-8")
byte_msg_b64encoded = base64.b64encode(byte_msg)
return byte_msg_b64encoded.decode(encoding="UTF-8")
def create_message():
message = "To: " + MAIL_TO + "\n"
message += "From: " + MAIL_FROM + "\n"
message += "Subject: =?utf-8?B?" + encode("gmail api test") + "?=\n"
message += "Date: " + formatdate(localtime=True) + "\n"
message += "Content-Type: multipart/alternative; boundary=boundaryboundary\n\n"
message += "--boundaryboundary\n"
message += "Content-Type: text/plain; charset=UTF-8\n"
message += "Content-Transfer-Encoding: base64\n\n"
message += encode("Hello world!") + "\n\n"
message += "--boundaryboundary"
return message
print(create_message())
To: example@gmail.com
From: example@gmail.com
Subject: =?utf-8?B?Z21haWwgYXBpIHRlc3Q=?=
Date: Thu, 15 Mar 2018 01:23:45 +0100
Content-Type: multipart/alternative; boundary=boundaryboundary
--boundaryboundary
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
SGVsbG8gd29ybGQh
--boundaryboundary
请将上述请求正文保存为文本文件。作为示例,文件名为sample.txt
。
在这里,请注意" EOF"的文件。请不要在最后--boundaryboundary
之后休息。如果它在最后--boundaryboundary
之后中断,则不会收到正文。图像如下。
curl -s -X POST \
-H "Authorization: Bearer *****" \
-H "Content-Type: message/rfc822" \
--data-binary "@sample.txt" \
"https://www.googleapis.com/upload/gmail/v1/users/me/messages/send"
它将sample.txt
作为二进制数据发布。
{
"id": "#####",
"threadId": "#####",
"labelIds": [
"UNREAD",
"SENT",
"INBOX"
]
}
如果我误解了你的问题,我很抱歉。