Use Tweepy functions for authentication and login

This commit is contained in:
Manuel Cortez 2020-07-20 13:11:37 -05:00
parent a89cc35d40
commit 13f5df3a48

View File

@ -12,7 +12,8 @@ import config
import output import output
import application import application
from pubsub import pub from pubsub import pub
from twython import Twython, TwythonError, TwythonRateLimitError, TwythonAuthError from twython import Twython
import tweepy
from mysc.thread_utils import call_threaded from mysc.thread_utils import call_threaded
from keys import keyring from keys import keyring
from sessions import base from sessions import base
@ -115,7 +116,9 @@ class Session(base.baseSession):
if self.settings["twitter"]["user_key"] != None and self.settings["twitter"]["user_secret"] != None: if self.settings["twitter"]["user_key"] != None and self.settings["twitter"]["user_secret"] != None:
try: try:
log.debug("Logging in to twitter...") log.debug("Logging in to twitter...")
self.twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), self.settings["twitter"]["user_key"], self.settings["twitter"]["user_secret"]) self.auth = tweepy.OAuthHandler(keyring.get("api_key"), keyring.get("api_secret"))
self.auth.set_access_token(self.settings["twitter"]["user_key"], self.settings["twitter"]["user_secret"])
self.twitter = tweepy.API(self.auth)
if verify_credentials == True: if verify_credentials == True:
self.credentials = self.twitter.verify_credentials() self.credentials = self.twitter.verify_credentials()
self.logged = True self.logged = True
@ -134,19 +137,18 @@ class Session(base.baseSession):
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:
twitter = Twython(keyring.get("api_key"), keyring.get("api_secret")) self.auth = tweepy.OAuthHandler(keyring.get("api_key"), keyring.get("api_secret"))
self.auth = twitter.get_authentication_tokens(callback_url="oob") redirect_url = self.auth.get_authorization_url()
webbrowser.open_new_tab(self.auth['auth_url']) webbrowser.open_new_tab(redirect_url)
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)
self.authorisation_dialog.ok.Bind(wx.EVT_BUTTON, self.authorisation_accepted) self.authorisation_dialog.ok.Bind(wx.EVT_BUTTON, self.authorisation_accepted)
self.authorisation_dialog.ShowModal() self.authorisation_dialog.ShowModal()
def verify_authorisation(self, pincode): def verify_authorisation(self, pincode):
twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), self.auth['oauth_token'], self.auth['oauth_token_secret']) self.auth.get_access_token(pincode)
final = twitter.get_authorized_tokens(pincode) self.settings["twitter"]["user_key"] = self.auth.access_token
self.settings["twitter"]["user_key"] = final["oauth_token"] self.settings["twitter"]["user_secret"] = self.auth.access_token_secret
self.settings["twitter"]["user_secret"] = final["oauth_token_secret"]
self.settings.write() self.settings.write()
del self.auth del self.auth