,则在结尾处有2个附加连字符
在用户授权"读取"权限后,我需要获取用户的所有电子邮件。我有access_token/refresh_token,可以帮助我为每封电子邮件单独打电话。我想对所有message_id进行批量请求,以减少所涉及的时间。这是我的批量请求失败的代码。
import httplib,urllib
def fetch_batch():
headers = {'Authorization' : 'Bearer ya29.swCasdjkfgsdalkfgsadfgasdjhgasdkjfasgdfaksdf', 'Host' : 'www.googleapis.com', 'Content-Type' : 'multipart/mixed; boundary=demoabc_wp'}
body="""demoabc_wp
Content-Type: application/http
GET /gmail/v1/users/uabc.kp1@gmail.com/messages/1497474ajsd
demoabc_wp
Content-Type: application/http
GET /gmail/v1/users/uabc.kp1@gmail.com/messages/149744safdg
demoabc_wp
"""
h = httplib.HTTPConnection('www.googleapis.com')
h.request('POST', '/batch', body, headers)
print h.getresponse().read()
这给出了以下响应:
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
提前谢谢。请参阅:https://developers.google.com/gmail/api/guides/batch和批量获取消息性能
您把多部分编码搞砸了。尝试
import httplib,urllib
def fetch_batch():
headers = {'Authorization' : 'Bearer ya29.swCasdjkfgsdalkfgsadfgasdjhgasdkjfasgdfaksdf', 'Host' : 'www.googleapis.com', 'Content-Type' : 'multipart/mixed; boundary=demoabc_wp'}
body="""--demoabc_wp
Content-Type: application/http
GET /gmail/v1/users/uabc.kp1@gmail.com/messages/1497474ajsd
--demoabc_wp
Content-Type: application/http
GET /gmail/v1/users/uabc.kp1@gmail.com/messages/149744safdg
--demoabc_wp--
"""
h = httplib.HTTPSConnection('www.googleapis.com')
h.request('POST', '/batch', body, headers)
print h.getresponse().read()
注意,在任何边界的开始处都有2个连字符,如果它是最后一个边界
Google API Python客户端库提供了对批处理请求的内置支持,从长远来看应该是一个更容易维护的解决方案。