GMail API会随机返回错误的错误 [英] GMail API returns unspeficied error at random moments

查看:42
本文介绍了GMail API会随机返回错误的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Google App Engine上运行一个Python应用程序,该应用程序会定期检查多个用户的最新电子邮件.我注意到,在随机时刻,API返回以下错误:

I'm running a Python app on Google App Engine which regularly checks the latest emails for multiple users. I've noticed that at random moments, the API returns the following error:

error: An error occured while connecting to the server: Unable to fetch URL: https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest?userIp=0.1.0.2

通常情况下,如果作业再次运行,它将起作用.在我的代码下面.我想了解是什么原因造成的,更重要的是我如何防止错误阻止进程(例如,抛出此错误时如何以相同的值再次运行作业).

Typically, if the job runs again, it works. Below my code. I would like to understand what is causing this, and more importantly how I can prevent the error from blocking the process (e.g. how to run the job again with the same values when this error is thrown).

class getLatest(webapp2.RequestHandler):
  def post(self):
    try:
      email = self.request.get('email')
      g = Credentials.get_by_id(email)
      REFRESH_TOKEN = g.refresh_token
      start_history_id = g.hid

      credentials = OAuth2Credentials(None, settings.CLIENT_ID,
                             settings.CLIENT_SECRET, REFRESH_TOKEN, None,
                             GOOGLE_TOKEN_URI, None,
                             revoke_uri=GOOGLE_REVOKE_URI,
                             id_token=None,
                             token_response=None)

      http = credentials.authorize(httplib2.Http())
      service = discovery.build("gmail", "v1", http=http)
      for n in range(0, 5): 
        try:
          history = service.users().history().list(userId=email, startHistoryId=start_history_id).execute(http=http)
          break
        except errors.HttpError, e:
          if n < 4:
            time.sleep((2 ** n) + random.randint(0, 1000) / 1000)
          else:
            raise
      changes = history['history'] if 'history' in history else []
      while 'nextPageToken' in history:
        page_token = history['nextPageToken']
        for n in range(0, 5): 
          try:
            history = service.users().history().list(userId=email, startHistoryId=start_history_id, pageToken=page_token).execute(http=http)
            break
          except errors.HttpError, e:
            if n < 4:
              time.sleep((2 ** n) + random.randint(0, 1000) / 1000)
            else:
              raise
        changes.extend(history['history'])

    except errors.HttpError, error:
        logging.exception('An error occurred: '+str(error))
        if error.resp.status == 401:
            # Credentials have been revoked.
            # TODO: Redirect the user to the authorization URL.
            raise NotImplementedError()
        else:
            stacktrace = traceback.format_exc()
            logging.exception('%s', stacktrace)

更新

我根据以下答案更新了代码,但是似乎从未多次运行请求.一旦发生异常,该过程就会中止.

I updated the code based on the answer below, however it never seems to run the request multiple times. The process just aborts as soon as an exception takes place.

推荐答案

事实证明,这与报告的此Stackoverflow问题中找到代码.

It turns out it was the same issue as reported here. I was hitting the rate limit on requesting the discovery document, which you only need to do once per day. I solved it by implementing the suggested solution on my app. The code can be found in this Stackoverflow question.

这篇关于GMail API会随机返回错误的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆