Get back to PIN code authorization due to Twitter changes. Closes #216

This commit is contained in:
Manuel Cortez 2018-06-06 09:11:37 -05:00
parent c2d81279fe
commit 9847f7ad01
3 changed files with 16 additions and 21 deletions

View File

@ -167,19 +167,19 @@ class Session(object):
if self.logged == True: if self.logged == True:
raise Exceptions.AlreadyAuthorisedError("The authorisation process is not needed at this time.") raise Exceptions.AlreadyAuthorisedError("The authorisation process is not needed at this time.")
else: else:
self.authorisation_thread = call_threaded(self.twitter.authorise, self.settings) self.twitter.authorise()
self.authorisation_dialog = authorisationDialog() self.authorisation_dialog = authorisationDialog()
self.authorisation_dialog.cancel.Bind(wx.EVT_BUTTON, self.authorisation_cancelled) self.authorisation_dialog.cancel.Bind(wx.EVT_BUTTON, self.authorisation_cancelled)
pub.subscribe(self.authorisation_accepted, "authorisation-accepted") self.authorisation_dialog.ok.Bind(wx.EVT_BUTTON, self.authorisation_accepted)
self.authorisation_dialog.ShowModal() self.authorisation_dialog.ShowModal()
def authorisation_cancelled(self, *args, **kwargs): def authorisation_cancelled(self, *args, **kwargs):
pub.sendMessage("authorisation-cancelled")
self.authorisation_dialog.Destroy() self.authorisation_dialog.Destroy()
del self.authorisation_dialog del self.authorisation_dialog
def authorisation_accepted(self): def authorisation_accepted(self, *args, **kwargs):
pub.unsubscribe(self.authorisation_accepted, "authorisation-accepted") pincode = self.authorisation_dialog.text.GetValue()
self.twitter.verify_authorisation(self.settings, pincode)
self.authorisation_dialog.Destroy() self.authorisation_dialog.Destroy()
def get_more_items(self, update_function, users=False, name=None, *args, **kwargs): def get_more_items(self, update_function, users=False, name=None, *args, **kwargs):

View File

@ -80,7 +80,9 @@ class authorisationDialog(wx.Dialog):
super(authorisationDialog, self).__init__(parent=None, title=_(u"Authorising account...")) super(authorisationDialog, self).__init__(parent=None, title=_(u"Authorising account..."))
panel = wx.Panel(self) panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL) sizer = wx.BoxSizer(wx.VERTICAL)
self.text = wx.TextCtrl(panel, -1, _("Waiting for account authorisation..."), style=wx.TE_READONLY|wx.TE_MULTILINE) static = wx.StaticText(panel, wx.NewId(), _(u"Enter your PIN code here"))
self.text = wx.TextCtrl(panel, -1)
self.ok = wx.Button(panel, wx.ID_OK)
self.cancel = wx.Button(panel, wx.ID_CANCEL) self.cancel = wx.Button(panel, wx.ID_CANCEL)
sizer.Add(self.text, 0, wx.ALL, 5) sizer.Add(self.text, 0, wx.ALL, 5)
sizer.Add(self.cancel, 0, wx.ALL, 5) sizer.Add(self.cancel, 0, wx.ALL, 5)

View File

@ -1,11 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import config import config
import random import random
import BaseHTTPServer
import webbrowser import webbrowser
from twython import Twython, TwythonError from twython import Twython, TwythonError
from keys import keyring from keys import keyring
import authorisationHandler
from requests import certs from requests import certs
import logging import logging
log = logging.getLogger("sessionTwitter") log = logging.getLogger("sessionTwitter")
@ -17,22 +15,17 @@ class twitter(object):
if verify_credentials == True: if verify_credentials == True:
self.credentials = self.twitter.verify_credentials() self.credentials = self.twitter.verify_credentials()
def authorise(self, settings): def authorise(self):
authorisationHandler.logged = False twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"))
port = random.randint(30000, 65535) self.auth = twitter.get_authentication_tokens(callback_url="oob")
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', port), authorisationHandler.handler) webbrowser.open_new_tab(self.auth['auth_url'])
twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), auth_endpoint='authorize')
auth = twitter.get_authentication_tokens("http://127.0.0.1:{0}".format(port,)) def verify_authorisation(self, settings, pincode):
webbrowser.open_new_tab(auth['auth_url']) self.twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), self.auth['oauth_token'], self.auth['oauth_token_secret'])
while authorisationHandler.logged == False: final = self.twitter.get_authorized_tokens(pincode)
httpd.handle_request()
self.twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), auth['oauth_token'], auth['oauth_token_secret'])
final = self.twitter.get_authorized_tokens(authorisationHandler.verifier)
self.save_configuration(settings, final["oauth_token"], final["oauth_token_secret"]) self.save_configuration(settings, final["oauth_token"], final["oauth_token_secret"])
httpd.server_close()
def save_configuration(self, settings, user_key, user_secret): def save_configuration(self, settings, user_key, user_secret):
settings["twitter"]["user_key"] = user_key settings["twitter"]["user_key"] = user_key
settings["twitter"]["user_secret"] = user_secret settings["twitter"]["user_secret"] = user_secret
settings.write() settings.write()