google-app-engine - Gmail REST API 线程搜索未给出预期结果

标签 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

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

最佳答案

这就是高级搜索的设计方式。 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)

关于google-app-engine - Gmail REST API 线程搜索未给出预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30110580/

相关文章:

java - GAE + Spring MVC : Saving image file to BlobStore

python - 谷歌应用引擎在 OSX Lion 上使用 python 失败

ios - 如何获取特定标签的 gmail 邮件,例如仅获取聊天邮件

google-api - 比 yyyy/mm/dd 更精确地列出消息

node.js - Nodemailer 使用原始用户帐户而不是别名发送电子邮件

python - 覆盖 ndb 属性的默认 _get_for_dict()

python - 如何在 GAE cron 作业中执行需要 OAuth 的操作?

java - 谷歌应用引擎: tasks vs threads?

javascript - 在 Google AppMaker 中发送电子邮件时出错

amazon-s3 - 如何将原始电子邮件 (MIME) 从 AWS SES 转换为 Gmail?