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

时间:2021-02-16 20:27:58

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.

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

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.

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

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


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

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

1 个解决方案

#1


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.

这就是高级搜索的设计方式。给出在12:00 AM(或00:00)之后发送的消息,之后在给定日期之前给出消息。要求:2015/04/28之前和之前:2015/04/28将导致不存在的时间跨度。

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)

我喜欢在以下后面使用备用表单: 。如果您想获得2015/04/28收到的所有信息,请写信:1430172000之前:1430258399(2015/04/28 00:00至2015/04/28 23:59:59)

#1


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.

这就是高级搜索的设计方式。给出在12:00 AM(或00:00)之后发送的消息,之后在给定日期之前给出消息。要求:2015/04/28之前和之前:2015/04/28将导致不存在的时间跨度。

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)

我喜欢在以下后面使用备用表单: 。如果您想获得2015/04/28收到的所有信息,请写信:1430172000之前:1430258399(2015/04/28 00:00至2015/04/28 23:59:59)