Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Gmail API“插入”一条“完整”格式的消息?_Python_Gmail Api - Fatal编程技术网

Python 如何使用Gmail API“插入”一条“完整”格式的消息?

Python 如何使用Gmail API“插入”一条“完整”格式的消息?,python,gmail-api,Python,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=**b

我成功地克隆了一条“原始”格式的消息,方法是使用
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()
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()
但是我很想使用json格式做同样的事情,
get
也可以通过
format='full'
返回。大概是这样的:

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

无法插入完整格式的邮件。如果使用/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.") } } { “消息”: { “raw”:base64url(“From:me\r\nTo:someguy\r\n对象:这里是\r\n\r\n空行后面的正文。”) } }
您可以使用附件,但可能需要一些mime电子邮件库来帮助您生成原始字段中编码为base64url的电子邮件字符串。

Ok,这是一个“您不能”的问题。原始方法和上载方法都不需要json格式。谢谢埃里克。@埃里克,。我可以用from、to、subject和body发送消息。但是,如何在发送消息时添加labelId和threadId?