Renew automatically access token if it has expired

This commit is contained in:
Manuel Cortez 2016-02-14 18:46:23 -06:00
parent 16c8750aa4
commit d018c440f6
4 changed files with 11 additions and 12 deletions

View File

@ -42,7 +42,7 @@ class Controller(object):
def login(self): def login(self):
self.window.change_status(_(u"Logging in VK")) self.window.change_status(_(u"Logging in VK"))
self.session.authorise() self.session.login()
self.window.change_status(_(u"Ready")) self.window.change_status(_(u"Ready"))
for i in self.buffers: for i in self.buffers:
if hasattr(i, "get_items"): if hasattr(i, "get_items"):

View File

@ -136,19 +136,18 @@ class vkSession(object):
if the user account isn't authorised, it needs to call self.authorise() before login.""" if the user account isn't authorised, it needs to call self.authorise() before login."""
if self.settings["vk"]["token"] != None: if self.settings["vk"]["token"] != None:
self.vk.login_access_token(self.settings["vk"]["token"]) result = self.vk.login_access_token(self.settings["vk"]["token"])
self.logged = True self.logged = True
log.debug("Logged.") log.debug("Logged.")
if result == False:
self.authorise()
else: else:
self.logged = False self.authorise()
raise Exceptions.RequireCredentialsSessionError
def authorise(self): def authorise(self):
if self.logged == True: self.vk.login(self.settings["vk"]["user"], self.settings["vk"]["password"])
raise Exceptions.AlreadyAuthorisedError("The authorisation process is not needed at this time.") self.settings["vk"]["token"] = self.vk.client._session.access_token
else: self.settings.write()
self.vk.login(self.settings["vk"]["user"], self.settings["vk"]["password"])
self.settings["vk"]["token"] = self.vk.client._session.access_token
def post_wall_status(self, message, *args, **kwargs): def post_wall_status(self, message, *args, **kwargs):
response = self.vk.client.wall.post(message=message, *args, **kwargs) response = self.vk.client.wall.post(message=message, *args, **kwargs)
@ -218,7 +217,6 @@ class vkSession(object):
def get_users(self, user_ids=None, group_ids=None): def get_users(self, user_ids=None, group_ids=None):
time.sleep(1) time.sleep(1)
print len(user_ids.split(","))
if user_ids != None: if user_ids != None:
u = self.vk.client.users.get(user_ids=user_ids, fields="uid, first_name, last_name") u = self.vk.client.users.get(user_ids=user_ids, fields="uid, first_name, last_name")
for i in u: for i in u:

View File

@ -15,3 +15,4 @@ class vkObject(object):
def login_access_token(self, token): def login_access_token(self, token):
s = Session(access_token=token) s = Session(access_token=token)
self.client = API(s) self.client = API(s)
return self.client.account.getProfileInfo()

View File

@ -93,7 +93,7 @@ class Session(object):
if AUTHORIZATION_FAILED in error_codes: # invalid access token if AUTHORIZATION_FAILED in error_codes: # invalid access token
logger.info('Authorization failed. Access token will be dropped') logger.info('Authorization failed. Access token will be dropped')
self.access_token = None self.access_token = None
return self.make_request(method_request) return False
else: else:
raise VkAPIMethodError(errors[0]) raise VkAPIMethodError(errors[0])