【问题标题】:Gmail REST API Thread Search not Giving Expected ResultsGmail REST API 线程搜索未给出预期结果
【发布时间】:2015-07-18 14:06:06
【问题描述】:

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

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

Gmail REST API 的文档既没有解释也没有强调这种行为。这是 API 的问题,还是有其他参数可以指定我们可以搜索这些电子邮件线程的时区?


您将在下面找到用于抓取此类电子邮件线程的 sn-p 代码:

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

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

【问题讨论】:

标签: google-app-engine gmail-api


【解决方案1】:

高级搜索就是这样设计的。 after 提供在上午 12:00(或 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)

【讨论】:

    猜你喜欢
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2016-01-30
    相关资源
    最近更新 更多