如何使用Gmail API“插入"完整格式的邮件? [英] How to `insert` a `full` format message using the Gmail API?

查看:134
本文介绍了如何使用Gmail API“插入"完整格式的邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地通过将getformat='raw'一起使用来克隆原始"格式的消息,并像这样重新插入它:

I've had success cloning a 'raw' formatted message by using get with format='raw' and re-inserting it just like this:

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格式执行相同的操作.像这样:

But I'm keen to do the same thing using the json format that get can alternatively return via format='full'. Something like this:

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]的格式,请参见下文. 这样做会给我错误:

See below for the format of mesg[1]. Doing the above gives me the error:

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

所以问题是:

格式为完整"而不是原始",因此我应该使用上传网址吗?如何? 我们是否以某种方式在raw或正文中继续使用JSON有效负载?
我们可以将其转换为原始格式,然后像以前一样进行操作吗?
我是否应该尝试找出如何以这种格式使用upload?
我应该放弃使用原始格式吗?
我会听到hear声回答这个问题吗?
这么多问题.

The format is 'full' not 'raw' so should I use the upload url? How? Do we continue with a json payload in the raw or body somehow?
Can we convert it to raw format then do same as before?
Should I try and work out how to use the upload with this format?
Should I give it up and work with the raw format?
Will I just hear crickets in response to this question?
So many questions.

邮件get文档在此处
邮件insert文档在此处

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

[1]完整格式get返回这种情况.这就是我希望以某种方式insert的方式.

[1] A full format get returns this kind of thing. And this is what I'm hoping to insert somehow.

{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,则只需发布以下内容:

You cannot insert a FULL formatted message. If you use the /upload URL then you need an uploadType string and you should have a content-type of message/rfc822. If you're not using /upload then you simply post something like:


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

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

you can use attachments but then you'll likely want some mime email libraries to help you generate that email message string that gets base64url encoded in the raw field.

这篇关于如何使用Gmail API“插入"完整格式的邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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