我使用了带有curl的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())
2条答案
按热度按时间n6lpvg4x1#
当使用
https://www.googleapis.com/upload/gmail/v1/users/me/messages/send
通过媒体上传请求发送消息时,需要创建请求正文,如下所示。我修改了您的python脚本来创建请求正文。请确认。修改的python脚本:
结果:
请将以上请求正文另存为文本文件。作为示例,文件名为
sample.txt
。要点:
这里,请注意文件的“EOF”位置。请不要在最后一个
--boundaryboundary
之后中断。如果在最后一个--boundaryboundary
之后中断,则无法接收正文。图像如下。curl 命令:
它将
sample.txt
作为二进制数据发布。结果:
备注:
如果我误解了你的问题,对不起。
olmpazwi2#
我想感谢其他有帮助的回答。我的用例要求我使用从Gmail获得的用户访问令牌,并回复给定的邮件线程。
在此,我不得不将Tanaike给出的上述答案修改为以下内容。
请注意我是如何使用用户的访问令牌构造所需凭据的;
这是从https://oauth2client.readthedocs.io/en/latest/source/oauth2client.client.html获取的