我在谷歌应用引擎上运行一个Python应用程序,它定期检查多个用户的最新电子邮件。我注意到,在随机时刻,API返回以下错误:
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
通常,如果作业再次运行,它就会工作。在我的代码下面。我想了解是什么导致了这种情况,更重要的是,我如何防止错误阻塞进程(例如,当抛出此错误时,如何使用相同的值再次运行作业)。
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)
更新
我根据下面的答案更新了代码,但它似乎从未多次运行请求。一旦发生异常,进程就会中止。
此错误可能是由于多种原因造成的,包括您的代理脱机一段时间或超过Gmail API的速率限制。如果出现这些临时问题,您需要以优雅的方式重试。以下是可能有帮助的代码编辑:
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 Exception as e:
# Apply exponential backoff.
time.sleep((2 ** n) + random.randint(0, 1000) / 1000)
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)
changes.extend(history['history'])
break
except Exception as e:
# Apply exponential backoff.
time.sleep((2 ** n) + random.randint(0, 1000) / 1000)
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)
从本质上讲,如果谷歌端从指数后退的同一点发生异常,则此代码会重试。添加您要重试的错误代码。希望这能解决你的问题。
事实证明,这与此处报告的问题相同。我在请求发现文档时达到了速率限制,您每天只需要这样做一次。我通过在我的应用程序上实现建议的解决方案解决了这个问题。代码可以在这个堆栈溢出问题中找到。