问题
I used Gmail API with curl.( Users.messages: send)
But I recieve Error 400 recipient address required.
Command
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"
Response
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "Recipient address required"
}
],
"code": 400,
"message": "Recipient address required"
}
}
The encoded value was created by the following python script.
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())
回答1:
When the messages are sent by the media upload requests using https://www.googleapis.com/upload/gmail/v1/users/me/messages/send
, the request body is required to be created as follows. I modified your python script for creating the request body. Please confirm it.
Modified python script :
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())
Result :
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
Please save above request body to a file as a text file. As a sample, the filename is sample.txt
.
Important point :
Here, please be careful the place of "EOF" of the file. Please don't break after the last --boundaryboundary
. If it breaks after the last --boundaryboundary
, the body is not received. The image is as follows.
Curl command :
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"
It posts sample.txt
as the binary data.
Result :
{
"id": "#####",
"threadId": "#####",
"labelIds": [
"UNREAD",
"SENT",
"INBOX"
]
}
Note :
- This is a very simple sample, so please modify this to your environment.
- This answer supposes that your access token can be used for this situation. If the error related to the access token occurs, please check the scopes.
If I misunderstand your question, I'm sorry.
来源:https://stackoverflow.com/questions/49290399/400-error-recipient-address-required-curl