python - 如何使用 Gmail API `insert` `full` 格式的邮件?

标签 python gmail-api

我已经通过使用 getformat='raw' 成功克隆了“原始”格式的消息,并像这样重新插入它:

params = {'userId':'me", 'id': msg_id, 'format':'raw'}
mesg = service.users().messages().get(**params).execute()

body = {'raw': mesg['raw']}
service.users().messages().insert(userId='me', body=**body).execute()

但我热衷于使用 get 可以通过 format='full' 返回的 json 格式做同样的事情。像这样:

params = {'userId':'me", 'id': msg_id, 'format':'full'}
mesg = service.users().messages().get(**params).execute()

body = mesg
service.users().messages().insert(userId='me', body=**body).execute()

mesg的格式见下文[1]。 执行上述操作会出现错误:

HttpError: <HttpError 400 when requesting 
  https://www.googleapis.com/gmail/v1/users/me/messages?alt=json 
  returned "
  'raw' RFC822 payload message string or uploading message via /upload/* 
   URL required
">

那么问题是:

如何通过“完整”json 格式插入消息?

格式是“完整”而不是“原始”,所以我应该使用上传网址吗?如何? 我们是否继续以某种方式在 raw 或正文中使用 json 负载?
我们可以将它转换为原始格式然后像以前一样做吗?
我应该尝试弄清楚如何使用这种格式的 upload 吗?
我应该放弃它并使用原始格式吗?
我会听到蟋蟀的叫声来回答这个问题吗?
这么多问题。


The messages get documentation is here
The messages insert documentation is here

[1] 完整格式的 get 返回这种东西。这就是我希望以某种方式插入的内容。

{u'historyId': u'5226', u'id': u'148af993efc00bce',
 u'snippet': u'Hi Joe Get the official Gmail app The best features of Gmail are only available on your phone and',
 u'sizeEstimate': 4809, u'threadId': u'148af993efc00bce', u'labelIds': [u'INBOX'],
 u'payload': {u'mimeType': u'multipart/alternative', u'headers': [
      {u'name': u'MIME-Version', u'value': u'1.0'},
      {u'name': u'x-no-auto-attachment', u'value': u'1'},
      {u'name':
           {u'historyId': u'5226',
            u'id': u'148af993efc00bce',
            u'snippet': u'Hi Joe Get the official Gmail app The best features of Gmail are only available on your phone and',
            u'sizeEstimate': 4809,
            u'threadId': u'148af993efc00bce',
            u'labelIds': [u'INBOX'], u'payload': {
               u'mimeType': u'multipart/alternative',
               u'headers': [{u'name': u'MIME-Version',
                             u'value': u'1.0'}, {
                                u'name': u'x-no-auto-attachment',
                                u'value': u'1'},
                            {u'name': u'Received',
                             u'value': u'by 10.31.41.213; Thu, 25 Sep 2014 18:35:28 -0700 (PDT)'},
                            {u'name': u'Date',
                             u'value': u'Thu, 25 Sep 2014 18:35:28 -0700'},
                            {u'name': u'Message-ID',
                             u'value': u'<CAJvL7e8jz9WYNUjHgnmYcyFgySXxjLiH1zjMxOfopURZmAy4iA@mail.gmail.com>'},
                            {u'name': u'Subject',
                             u'value': u'The best of Gmail, wherever you are'},
                            {u'name': u'From',
                             u'value': u'Gmail Team <mail-noreply@google.com>'},
                            {u'name': u'To',
                             u'value': u'Joe Test <test@gmail.com>'},
                            {u'name': u'Content-Type',
                             u'value': u'multipart/alternative; boundary=bcaec547c84f9cba4a0503edee6b'}],
               u'parts': [{u'mimeType': u'text/plain',
                           u'headers': [
                               {u'name': u'Content-Type',
                                u'value': u'text/plain; charset=UTF-8'},
                               {
                                   u'name': u'Content-Transfer-Encoding',
                                   u'value': u'quoted-printable'}],
                           u'body': {
                               u'data': u'IFRoZSBiZXN0IG9mIEdtYWlsLCB3aGVyZ...

最佳答案

您不能插入完整格式的消息。如果你使用/upload URL,那么你需要一个 uploadType 字符串,你应该有一个 message/rfc822 的内容类型。如果您不使用/upload,那么您只需发布类似以下内容:

{
  'message': 
  {
     'raw': base64url("From: me\r\nTo: someguy\r\nSubject: here it is\r\n\r\nbody after blank line.")
  }
}

您可以使用附件,但您可能需要一些 mime 电子邮件库来帮助您生成在原始字段中进行 base64url 编码的电子邮件消息字符串。

关于python - 如何使用 Gmail API `insert` `full` 格式的邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487630/

相关文章:

javascript - Google Scripts - 从退回的邮件中获取电子邮件地址并解析信息

python - 查找边界之间错误颜色的像素

gmail - 如何从 Gmail API 获取没有原始邮件的回复邮件

gmail-api - 与日期查询的 Gmail Web UI 相比,Gmail API 未返回正确的电子邮件

python - 选择特定的字典集

javascript - 如何使用nodejs的google-api-nodejs-client在gmail api中发送带有附件的邮件

django - 在 django 中使用 gmail api

python - 如何更改管理端口?

python - Visual Studio Code 告诉我它不能导入 python 模块,但它运行代码

python - Numpy:ImportError:无法导入名称 TestCase