400错误.收件人地址为必填项.卷曲 [英] 400 Error. Recipient address required. curl

查看:51
本文介绍了400错误.收件人地址为必填项.卷曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了带有curl的Gmail API.(

卷曲命令:

  curl -s -X POST \-H授权:承载*****"-H内容类型:message/rfc822"--data-binary"@ sample.txt" \"https://www.googleapis.com/upload/gmail/v1/users/me/messages/send" 

它将 sample.txt 作为二进制数据发布.

结果:

  {ID": "#####","threadId":"#####","labelIds":[未读",发送",收件箱"]} 

注意:

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

如果我误解了你的问题,对不起.

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())

解决方案

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.

这篇关于400错误.收件人地址为必填项.卷曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆