400 错误。需要收件人地址。卷曲

2024-04-20

我将 Gmail API 与curl一起使用。(用户.消息:发送 https://developers.google.com/gmail/api/v1/reference/users/messages/send)

但我收到错误 400,需要收件人地址。

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"
   }
 }


编码值是由以下 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 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())

当消息通过媒体上传请求发送时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 :

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

400 错误。需要收件人地址。卷曲 的相关文章

随机推荐

  • 使用 eval 加载模块

    我在 Perl 和内置函数方面遇到了一些麻烦eval http perldoc perl org functions eval html 我浏览了网络 但找不到任何答案或示例代码 我想动态加载模块 在执行时间之前我不知道它们 module
  • 嵌套 RibbonApplicationMenuItem 时出错

    我想建立一个RibbonApplicationMenu 其中应嵌套一个RibbonApplicationMenuItem or RibbonApplicationSplitMenuItem 例如喜欢这个
  • Azure Web 角色如何在没有入口点的情况下运行?

    出于好奇 我打开了我的 Azure Web 角色项目 导航到包含以下内容的文件 RoleEntryPoint后代阶级和完全删除了该类定义 然后我打包了该角色并将其部署在 Azure 中 该角色启动时没有任何错误指示 这怎么可能行得通 除了
  • 如何找出 WPF 应用程序中的焦点在哪里?

    我的 WPF 应用程序中有一个搜索屏幕 该屏幕作为 TabControl 的 TabItem 中的 UserControl 实现 当用户切换到 搜索 选项卡时 我希望焦点进入一个特定字段 因此 我向 Xaml 中的 UserControl
  • AKSequencer 计数一或两个小节

    在当前序列开始播放之前需要播放 1 或 2 个小节进行计数 只需点击一下即可计入 能够做类似的事情会很酷 player sequencer setTime MusicTimeStamp 4 将时间设置为0 不起作用 使用 AKSequenc
  • 如何计算scipy中曲线拟合的可能性?

    我有一个非线性模型拟合 如下所示 深色实线是模型拟合 灰色部分是原始数据 问题的简短版本 如何获得该模型拟合的可能性 以便我可以执行对数似然比测试 假设残差服从正态分布 我对统计学比较陌生 我目前的想法是 从曲线拟合中得到残差 并计算残差的
  • YouTube 视频 ID 的最大长度是多少?

    我正在开发一个显示 YouTube 视频的应用程序 我想将视频 id 存储在数据库中 但是因为会有很多视频 我想最小化所需的空间 所以有人知道 youtube 上视频 id 的最大长度吗 几乎可以肯定它会保持在 11 个字符 各个字符来自一
  • 是否可以创建一个不带参数的 C 可变参数函数? [复制]

    这个问题在这里已经有答案了 可能的重复 C 中是否可以有一个没有非可变参数的可变参数函数 https stackoverflow com questions 2622147 is it possible to have a variadic
  • 按渐近增长率对函数排序

    按渐近增长率的非降序列出以下函数 如果两个或多个函数具有相同的渐近增长率 则将它们分组在一起 g1 n n g2 n n 3 4n g3 n 2n log 以 2 为底 n g4 n 2 n g5 n 3 3 log 以 3 为底 n g6
  • 在 JSF 中使用消息束时从验证消息中删除组件 ID

    我正在练习JSF 我创建了一个带有用户 ID 和密码字段的登录屏幕 现在两个输入字段都需要 true 我创建了 message properties 文件并向其中添加了以下验证 ID javax faces component UIInpu
  • Codeigniter:使用 RESTful 服务

    是否有一个易于设置的 Codeigniter REST 库 我可以用它来使用 RESTful 服务 我尝试设置this https github com philsturgeon codeigniter restclient图书馆 但无法设
  • 无法在 macOS High Sierra 上打开 SQL 编辑器

    自从更新到 macOS High Sierra 以来 我一直无法在 MySQL Workbench 上打开 SQL 编辑器 When I try to connect to the database as usual I m given t
  • 如何获取真实的UTC时间戳(与客户端无关)?

    有没有办法获得real当前 UTC 时间戳而不依赖于客户端时间 这可能无法在每个客户端上正确设置 我知道 JavaScript 的getTime 我可以获取以毫秒为单位的 UTC 时间戳 它独立于客户的时区 但我认为它取决于客户的当前时间
  • CoTaskMemAlloc v malloc v AllocHGlobal

    我读过 在 Windows 上 malloc 与 CoTaskMemAlloc 不同 CoTaskMemAlloc 与 AllocHGlobal 不同 对于 C 用户来说 这意味着如果我有一个返回 malloc 指针的 C 函数 我需要对其
  • Android SeekBar 最小和连续浮点值属性

    我正在寻找一种在 SeekBar 中实现最小值的方法 并且还可以选择增加十进制数字 例如 当前我的 SeekBar 的最小值设置为 0 但我需要它从值 0 2 开始 另外 我希望能够让用户以 0 1 的精度选择 0 2 到 10 0 之间的
  • 如何理解Cassandra中的“灵活模式”?

    我是 Cassandra 的新手 可以在下面的维基百科中找到 列族 自 CQL 3 起称为 表 类似于 RDBMS 关系数据库管理系统 中的表 列族包含行和列 每行都由行键唯一标识 每行有多列 每列都有名称 值和时间戳 与 RDBMS 中的
  • 在 Windows 10 和 PHP 7.3 中安装 AMQP

    我想在 Windows 10 中使用 PHP 7 3 安装 AMQP 以便在 symfony 4 中使用 Windows 不使用任何 apache iis nginx 并直接由 symfony 运行 一切还好 直到 我决定在项目中使用rab
  • ggplot2:将面/条文本分割成两行

    考虑以下带有长面 条带文本的 ggplot2 图 断成两行 该文本超出了专门用于分面标题的区域 library ggplot2 x lt c 1 3 1 3 y lt c 3 1 1 3 grp lt c 0 0 0 1 1 1 p lt
  • Javascript:作用域链何时创建?

    我听到过两种说法 当定义函数时 In book Professional Javascript for Web Developers 3rd Edition 在里面Chapter 7 Function Expressions封闭部件 当co
  • 400 错误。需要收件人地址。卷曲

    我将 Gmail API 与curl一起使用 用户 消息 发送 https developers google com gmail api v1 reference users messages send 但我收到错误 400 需要收件人地