0
我在GMail API上運行了許多任務,並且遇到了與this issue中描述的相同的錯誤。要解決它,我想實施建議的解決方案。不過,我不知道如何將其應用於我的代碼。GAE上的Gmail API:正確使用discovery.build_from_document()
我的代碼目前看起來如下:
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)