情景:
當授予訪問權限時,我爲每個應用用戶在數據存儲中保存令牌。gdata OAuth2如何使用多個用戶的令牌
我需要創建多個的GData聯繫,以便授權客戶 訪問不同用戶的接觸中,我得到令牌的用戶需要從 數據存儲和創建授權接觸客戶,例如將一個用戶的 聯繫人複製到其他用戶的聯繫人。
問題:
授權客戶按預期工作,並得到了 用戶,其令牌使用的接觸飼料,但令牌,令牌到期日也 保存,並在一個小時或45到期分鐘,然後它不再工作, 我讀到,如果刷新令牌存在於令牌對象中,則令牌 將自動獲得新的訪問令牌,如果短期訪問令牌爲 已過期但不會發生。
問:
我怎麼能刷新令牌從數據存儲得到它,如果它是 過期,所以我可以,只要用戶沒有 取消獲得訪問用戶的聯繫人後?
CODE:
SCOPE = 'https://www.google.com/m8/feeds' REDIRECT_URI = 'http://localhost:8888/mainPage' USER_AGENT = 'shared-contacts' token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope=SCOPE, user_agent=USER_AGENT) class MyEntity(db.Model): userEmail = db.StringProperty() access_token = ObjectProperty() class ObjectProperty(db.BlobProperty): def validate(self, value): try: result = pickle.dumps(value) return value except pickle.PicklingError, e: return super(ObjectProperty, self).validate(value) def get_value_for_datastore(self, model_instance): result = super(ObjectProperty, self).get_value_for_datastore(model_instance) result = pickle.dumps(result) return db.Blob(result) def make_value_from_datastore(self, value): try: value = pickle.loads(str(value)) except: pass return super(ObjectProperty, self).make_value_from_datastore(value) class MainHandler(webapp2.RequestHandler): def get(self): user = users.get_current_user() if user: self.redirect(token.generate_authorize_url( redirect_uri=REDIRECT_URI, access_type='offline') ) else: self.redirect(users.create_login_url("/")) class MainPage(webapp2.RequestHandler): def get(self): # Getting the token from datastore, if token exists for this user then # proceed otherwise get the access token and save in datastore. accessToken = Fetch_Access_Token() if accessToken: pass else: url = atom.http_core.Uri.parse_uri(self.request.uri) code = url.query code = code['code'] if 'error' in code: # User Did Not Grant The Access pass else: token_user_email = users.get_current_user().email() token.GetAccessToken(code) entity = MyEntity(userEmail=token_user_email, access_token=token) entity.put() self.response.out.write(template.render('index.html',{})) class RPCMethods(): def MoveContacts(self, *args): # All tokens except current user shared_group_users = shared_users(skip_current_user) contact_client = gdata.contacts.client.ContactsClient(source=USER_AGENT) for sg_user in shared_group_users.itervalues(): shared_user_token = sg_user[1] # PROBLEM OCCURS HERE, IT WORKS FOR ONLY 1 HOUR AND HERE I NEED TO REFRESH THIS TOKEN. # For almost an hour i get other user's Data, but after that i get current user's Data. shared_user_authorized_client = shared_user_token.authorize(contact_client) shared_group_contact_feed = shared_user_authorized_client.GetContacts() application = webapp2.WSGIApplication([ ('/', MainHandler), ('/mainPage', MainPage), ('/rpc', RPCHandler), ], debug=True)
UPDATE:
工作代碼:
shared_user_credentials = StorageByKeyName(CredentialsModel, shared_user_credentials_key, 'credentials').get()
if shared_user_credentials.access_token_expired == True:
http = httplib2.Http()
http = shared_user_credentials.authorize(http)
shared_user_credentials.refresh(http)
如何將其存儲在數據存儲中?你在執行什麼來獲取'atom.http_core.Uri.parse_uri(self.request.uri)'? – bossylobster 2013-02-11 17:58:33
@bossylobster 我使用gdata.gauth.OAuth2Token得到的令牌對象,我無法使用應用程序引擎中提供的方法將它保存在數據存儲中 例如, ae_save或 datastore CredentialsProperty(),所以我搜索n找到了一個解決方案。 回答你的問題太長了評論,所以我編輯了 的問題,並添加了所有的代碼,請看看,謝謝 – 2013-02-12 13:45:38