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):
self.window.change_status(_(u"Logging in VK"))
self.session.authorise()
self.session.login()
self.window.change_status(_(u"Ready"))
for i in self.buffers:
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 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
log.debug("Logged.")
if result == False:
self.authorise()
else:
self.logged = False
raise Exceptions.RequireCredentialsSessionError
self.authorise()
def authorise(self):
if self.logged == True:
raise Exceptions.AlreadyAuthorisedError("The authorisation process is not needed at this time.")
else:
self.vk.login(self.settings["vk"]["user"], self.settings["vk"]["password"])
self.settings["vk"]["token"] = self.vk.client._session.access_token
self.vk.login(self.settings["vk"]["user"], self.settings["vk"]["password"])
self.settings["vk"]["token"] = self.vk.client._session.access_token
self.settings.write()
def post_wall_status(self, 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):
time.sleep(1)
print len(user_ids.split(","))
if user_ids != None:
u = self.vk.client.users.get(user_ids=user_ids, fields="uid, first_name, last_name")
for i in u:

View File

@ -14,4 +14,5 @@ class vkObject(object):
def login_access_token(self, 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
logger.info('Authorization failed. Access token will be dropped')
self.access_token = None
return self.make_request(method_request)
return False
else:
raise VkAPIMethodError(errors[0])