GAE 上的 Gmail API:正确使用 discovery.build_from_document()
Gmail API on GAE: Correctly using discovery.build_from_document()
我在 GMail API 上 运行 执行多项任务,但遇到了与 this issue 中所述相同的错误。要解决它,我想实施建议的解决方案。但是我不确定如何将其应用到我的代码中。
我的代码目前如下所示:
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)
现在我应该 运行 在某个时刻遵循代码:
resp, content = h.request('https://www.googleapis.com/discovery/v1/apis/analytics/v3/rest?quotaUser=the_name_of_your_app_goes_here')
然后将内容的值保存在数据存储中。这个问题解释说这不是用户绑定的,但是我不清楚我是否应该 运行 这一次并将值存储到永恒,或者在某些时刻及时刷新它。
此外,由于我处理授权的方式有点不同,我觉得如果我以完全相同的方式实施授权,我也会 运行 解决那里的问题。因为当我构建服务时,我实际上在调用中添加了凭据:
http = credentials.authorize(httplib2.Http())
service = discovery.build("gmail", "v1", http=http)
事实证明这很简单。我添加了以下功能:
def get_discovery_content():
content = memcache.get('gmail_discovery')
if content is not None:
return content
else:
h = httplib2.Http()
for n in range(0, 5):
resp, content = h.request('https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest?quotaUser=replimeapp')
if resp.status == 200:
memcache.add('gmail_discovery', content, 86400)
return content
然后我替换了这一行:
service = discovery.build("gmail", "v1", http=http)
由此:
content = get_discovery_content()
service = discovery.build_from_document(content)
到目前为止效果很好。
我在 GMail API 上 运行 执行多项任务,但遇到了与 this issue 中所述相同的错误。要解决它,我想实施建议的解决方案。但是我不确定如何将其应用到我的代码中。
我的代码目前如下所示:
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)
现在我应该 运行 在某个时刻遵循代码:
resp, content = h.request('https://www.googleapis.com/discovery/v1/apis/analytics/v3/rest?quotaUser=the_name_of_your_app_goes_here')
然后将内容的值保存在数据存储中。这个问题解释说这不是用户绑定的,但是我不清楚我是否应该 运行 这一次并将值存储到永恒,或者在某些时刻及时刷新它。
此外,由于我处理授权的方式有点不同,我觉得如果我以完全相同的方式实施授权,我也会 运行 解决那里的问题。因为当我构建服务时,我实际上在调用中添加了凭据:
http = credentials.authorize(httplib2.Http())
service = discovery.build("gmail", "v1", http=http)
事实证明这很简单。我添加了以下功能:
def get_discovery_content():
content = memcache.get('gmail_discovery')
if content is not None:
return content
else:
h = httplib2.Http()
for n in range(0, 5):
resp, content = h.request('https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest?quotaUser=replimeapp')
if resp.status == 200:
memcache.add('gmail_discovery', content, 86400)
return content
然后我替换了这一行:
service = discovery.build("gmail", "v1", http=http)
由此:
content = get_discovery_content()
service = discovery.build_from_document(content)
到目前为止效果很好。