Search code examples
oauth-2.0google-apibatch-processingurllibhttplib

How to do Google API batch request using python httplib or urllib2?


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


Solution

  • 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