如何使用Gmail API插入完整格式消息?
我成功地使用 get
方法和 format='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
方法的 json 格式来做同样的事情,可以通过 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()
下面是 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
来处理这个格式吗?
还是应该放弃,继续使用原始格式?
我会不会对这个问题只听到寂静?
有太多问题了。
关于消息 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...
1 个回答
4
你不能直接插入一个完整格式的消息。如果你使用的是 /upload 这个网址,那么你需要一个 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 编码的邮件消息字符串。