Eros Deva
Eros Deva

Reputation: 21

Gmail REST API Thread Search not Giving Expected Results

We have built an Email Audit Application for one of our customers. The app utilizes the Gmail REST API and provides a front-end interface that allows users with permission to run audit queries based on a selected date-ranges. The date-range that is provided by the user is utilized in the Email Thread search query.

We have noticed, however, that the API is showing a discrepancy between the threads that are returned and the actual items that are in the inbox of an audited user. For example, in order to collect everything within 1 day, say 4/28, we need to expand the audit range from 4/27-4/29.

The documentation for the Gmail REST API provides no explanation nor highlighting of this behavior. Is this an issue with the API or are there additional parameters that perhaps can specify the time-zone for which we can search for these email threads?


Below you will find a snippet of code that is utilized to grab such email threads:

def GrabAllThreadIDs(user_email, after_date, before_date):

   query = "in:inbox " + "after:" + after_date + " " + "before:" + before_date
   # Create the Gmail Service
   gmail_service = create_gmail_service(user_email)
   raw_thread_response = ListThreadsMatchingQuery(gmail_service, 'me', query)

   for item in raw_thread_response:
      all_ids.append(item['id'])

   return all_ids

======================================================

def ListThreadsMatchingQuery(service, user_id, query=''):
    """List all Threads of the user's mailbox matching the query.

    Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    query: String used to filter messages returned.
    Eg.- 'label:UNREAD' for unread messages only.

    Returns:
    List of threads that match the criteria of the query. Note that the returned
    list contains Thread IDs, you must use get with the appropriate
    ID to get the details for a Thread.
    """
try:
    response = service.users().threads().list(userId=user_id, q=query).execute()
    threads = []
    if 'threads' in response:
        threads.extend(response['threads'])

    while 'nextPageToken' in response:
        page_token = response['nextPageToken']
        response = service.users().threads().list(userId=user_id, q=query,
        pageToken=page_token).execute()
        threads.extend(response['threads'])

    return threads
except errors.HttpError, error:
    print 'An error occurred: %s' % error

======================================================

Upvotes: 2

Views: 369

Answers (1)

Tholle
Tholle

Reputation: 112787

That is how the Advanced search is designed. after gives messages sent after 12:00 AM (or 00:00), and before gives messages before the given date. Asking for after:2015/04/28 and before:2015/04/28 would result in a non-existent timespan.

I like to use the alternate form after:<TIME_IN_SECONDS_SINCE_THE_EPOCH>. If you would like to get all the messages received on 2015/04/28 you would write after:1430172000 before:1430258399 (2015/04/28 00:00 to 2015/04/28 23:59:59)

Upvotes: 2

Related Questions