当消息通过媒体上传请求发送时https://www.googleapis.com/upload/gmail/v1/users/me/messages/send
,请求体需要创建如下。我修改了您的 python 脚本来创建请求正文。请证实。
修改后的python脚本:
import base64
from email.mime.text import MIMEText
from email.utils import formatdate
MAIL_FROM = "[email protected] /cdn-cgi/l/email-protection"
MAIL_TO = "[email protected] /cdn-cgi/l/email-protection"
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())
Result :
To: [email protected] /cdn-cgi/l/email-protection
From: [email protected] /cdn-cgi/l/email-protection
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
作为二进制数据。
Result :
{
"id": "#####",
"threadId": "#####",
"labelIds": [
"UNREAD",
"SENT",
"INBOX"
]
}
Note :
- 这是一个非常简单的示例,因此请根据您的环境进行修改。
- 此答案假设您的访问令牌可用于这种情况。如果发生与访问令牌相关的错误,请检查范围。
如果我误解了你的问题,我很抱歉。