【问题标题】:400 Error. Recipient address required. curl400 错误。需要收件人地址。卷曲
【发布时间】: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())

【问题讨论】:

    标签: curl gmail-api


    【解决方案1】:

    当媒体上传请求使用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 = "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"
     ]
    }
    

    注意:

    • 这是一个非常简单的示例,因此请根据您的环境进行修改。
    • 此答案假定您的访问令牌可用于这种情况。如果出现访问令牌相关的错误,请检查范围。

    如果我误解了你的问题,我很抱歉。

    【讨论】:

      猜你喜欢
      • 2018-08-14
      • 1970-01-01
      • 2021-10-23
      • 2019-09-21
      • 2019-10-30
      • 2017-04-30
      • 2010-11-21
      • 2020-08-23
      • 2015-10-31
      相关资源
      最近更新 更多