【问题标题】:How to `insert` a `full` format message using the Gmail API?如何使用 Gmail API 插入“完整”格式的邮件?
【发布时间】:2014-12-16 17:35:21
【问题描述】:

通过使用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 格式insert 发送消息?

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


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

[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...

【问题讨论】:

    标签: python gmail-api


    【解决方案1】:

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

    { '信息': { 'raw': base64url("From: me\r\nTo: someguy\r\nSubject: 这里是\r\n\r\nbody 在空行之后。") } }

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

    【讨论】:

    • 好吧,那是“你不能”。 raw 或 upload 方法都不需要 json 格式。谢谢埃里克。
    • @eric,。我可以用 from、to、subject、body 发送消息。但是,如何在发送消息时添加labelId和threadId?
    猜你喜欢
    • 2015-01-22
    • 2019-02-24
    • 2016-06-01
    • 1970-01-01
    • 2019-06-02
    • 2017-02-11
    • 1970-01-01
    • 2016-09-28
    • 2016-07-26
    相关资源
    最近更新 更多