Gmail REST API线程搜索未提供预期结果

时间:2015-05-07 19:59:09

标签: google-app-engine gmail-api

我们已为其中一位客户构建了电子邮件审核应用程序。该应用程序使用Gmail REST API并提供前端界面,允许具有权限的用户根据选定的日期范围运行审核查询。用户提供的日期范围在电子邮件线程搜索查询中使用。

但是,我们注意到API显示返回的线程与审核用户的收件箱中的实际项目之间存在差异。例如,为了在1天内收集所有信息,比如4/28,我们需要将审计范围从4 / 27-4 / 29扩大。

Gmail REST API的文档不提供任何解释,也不会突出显示此行为。这是API的问题还是有其他参数可能指定我们可以搜索这些电子邮件线程的时区?

您将在下面找到一段用于获取此类电子邮件主题的代码:

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

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

1 个答案:

答案 0 :(得分:2)

这就是高级搜索的设计方式。 after提供在12:00 AM(或00:00)之后发送的消息,before在给定日期之前发送消息。询问after:2015/04/28before:2015/04/28会导致不存在的时间跨度。

我喜欢使用替代形式after:<TIME_IN_SECONDS_SINCE_THE_EPOCH>。如果您想收到2015/04/28收到的所有信息,请写after:1430172000 before:1430258399(2015/04/28 00:00至2015/04/28 23:59:59)