如何使用 Gmail API“插入”一个“完整”格式的邮件?

Joh*_*Mee 5 python gmail-api

我已经成功克隆了“原始”格式的消息,方法是使用getwithformat='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()
Run Code Online (Sandbox Code Playgroud)

但是我很想使用 json 格式来做同样的事情,该格式get可以通过format='full'. 像这样的东西:

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()
Run Code Online (Sandbox Code Playgroud)

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
">
Run Code Online (Sandbox Code Playgroud)

所以问题是:

如何insert通过“完整”json 格式发送消息?

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


消息get文档在这里
消息insert文档在这里

[1] 完整格式get返回这种东西。这就是我希望以insert某种方式实现的。

{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...
Run Code Online (Sandbox Code Playgroud)

Eri*_*c D 4

您无法插入完整格式的消息。如果您使用 /upload URL,则需要 uploadType 字符串,并且内容类型应该为 message/rfc822。如果您不使用 /upload 那么您只需发布类似以下内容:

{
  '信息':
  {
     'raw': base64url("发件人:我\r\n收件人:某个人\r\n主题:这里是\r\n\r\n空行后的正文。")
  }
}

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