0

After the user authorizes the 'read' permission, I need to fetch all the emails of the user. I have access_token/refresh_token which help me to make individual calls for each email. I want to do batch request for all message_ids to reduce the time involved. Here goes my code for batch request which fails.

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

This gives this as response:

<HTML>
<HEAD>
    <TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Bad Request</H1>
    <H2>Error 400</H2>
</BODY>
</HTML>

Thanks in advance. Refer from : https://developers.google.com/gmail/api/guides/batch and Batch fetching messages performance

Community
  • 1
  • 1
kawadhiya21
  • 2,458
  • 21
  • 34

2 Answers2

1

You messed up the multipart encoding. Try

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

Note, it is 2 hyphens at the start of any boundary, and 2 additional hyphens at the end if it is the last boundary

Andrew Leap
  • 956
  • 4
  • 9
  • I think you should try using the code to make a request. It is a 400 error. Although I used the exact syntax you suggested but still it shows a 400 error. There is something else I am missing I think. – kawadhiya21 Nov 04 '14 at 19:23
  • 2 Issues, needs to be https, and spaces before things in the body breaks stuff. Above works, except for complaining cause that's not a valid authorization. Note, even with those fixes, it still needs the hyphens – Andrew Leap Nov 05 '14 at 04:41
1

The Google APIs Python Client Library provides built-in support for batch requests, and should be an easier solution to maintain in the long run.

Eric Koleda
  • 12,420
  • 1
  • 33
  • 51