mirror of
				https://github.com/MCV-Software/TWBlue.git
				synced 2025-10-31 04:12:00 +00:00 
			
		
		
		
	Handle errors in authorisation for both Twitter and mastodon
This commit is contained in:
		| @@ -60,10 +60,15 @@ class Session(base.baseSession): | |||||||
|         authorisation_dialog.Destroy() |         authorisation_dialog.Destroy() | ||||||
|         if answer != wx.ID_OK: |         if answer != wx.ID_OK: | ||||||
|             return |             return | ||||||
|         client_id, client_secret = mastodon.Mastodon.create_app("TWBlue", api_base_url=authorisation_dialog.GetValue(), website="https://twblue.es") |         try: | ||||||
|         temporary_api = mastodon.Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=instance, mastodon_version=MASTODON_VERSION) |             client_id, client_secret = mastodon.Mastodon.create_app("TWBlue", api_base_url=authorisation_dialog.GetValue(), website="https://twblue.es") | ||||||
|         authorisation_dialog.Destroy() |             temporary_api = mastodon.Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=instance, mastodon_version=MASTODON_VERSION) | ||||||
|         auth_url = temporary_api.auth_request_url() |             auth_url = temporary_api.auth_request_url() | ||||||
|  |         except MastodonError: | ||||||
|  |             dlg = wx.MessageDialog(None, _("We could not connect to your mastodon instance. Please verify that the domain exists and the instance is accessible via a web browser."), _("Instance error"), wx.ICON_ERROR) | ||||||
|  |             dlg.ShowModal() | ||||||
|  |             dlg.Destroy() | ||||||
|  |             return | ||||||
|         webbrowser.open_new_tab(auth_url) |         webbrowser.open_new_tab(auth_url) | ||||||
|         verification_dialog = wx.TextEntryDialog(None, _("Enter the verification code"), _("PIN code authorization")) |         verification_dialog = wx.TextEntryDialog(None, _("Enter the verification code"), _("PIN code authorization")) | ||||||
|         answer = verification_dialog.ShowModal() |         answer = verification_dialog.ShowModal() | ||||||
| @@ -71,10 +76,19 @@ class Session(base.baseSession): | |||||||
|         verification_dialog.Destroy() |         verification_dialog.Destroy() | ||||||
|         if answer != wx.ID_OK: |         if answer != wx.ID_OK: | ||||||
|             return |             return | ||||||
|         access_token = temporary_api.log_in(code=verification_dialog.GetValue()) |         try: | ||||||
|  |             access_token = temporary_api.log_in(code=verification_dialog.GetValue()) | ||||||
|  |         except MastodonError: | ||||||
|  |             dlg = wx.MessageDialog(None, _("We could not authorice your mastodon account to be used in TWBlue. This might be caused due to an incorrect verification code. Please try to add the session again."), _("Authorization error"), wx.ICON_ERROR) | ||||||
|  |             dlg.ShowModal() | ||||||
|  |             dlg.Destroy() | ||||||
|  |             return | ||||||
|  |         self.create_session_folder() | ||||||
|  |         self.get_configuration() | ||||||
|         self.settings["mastodon"]["access_token"] = access_token |         self.settings["mastodon"]["access_token"] = access_token | ||||||
|         self.settings["mastodon"]["instance"] = instance |         self.settings["mastodon"]["instance"] = instance | ||||||
|         self.settings.write() |         self.settings.write() | ||||||
|  |         return True | ||||||
|  |  | ||||||
|     def get_user_info(self): |     def get_user_info(self): | ||||||
|         """ Retrieves some information required by TWBlue for setup.""" |         """ Retrieves some information required by TWBlue for setup.""" | ||||||
|   | |||||||
| @@ -5,13 +5,13 @@ import time | |||||||
| import logging | import logging | ||||||
| import webbrowser | import webbrowser | ||||||
| import wx | import wx | ||||||
|  | import tweepy | ||||||
| import demoji | import demoji | ||||||
| import config | import config | ||||||
| import output | import output | ||||||
| import application | import application | ||||||
| import appkeys | import appkeys | ||||||
| from pubsub import pub | from pubsub import pub | ||||||
| import tweepy |  | ||||||
| from tweepy.errors import TweepyException, Forbidden, NotFound | from tweepy.errors import TweepyException, Forbidden, NotFound | ||||||
| from tweepy.models import User as UserModel | from tweepy.models import User as UserModel | ||||||
| from mysc.thread_utils import call_threaded | from mysc.thread_utils import call_threaded | ||||||
| @@ -150,37 +150,32 @@ class Session(base.baseSession): | |||||||
|             self.logged = False |             self.logged = False | ||||||
|             raise Exceptions.RequireCredentialsSessionError |             raise Exceptions.RequireCredentialsSessionError | ||||||
|  |  | ||||||
| # @_require_configuration |  | ||||||
|     def authorise(self): |     def authorise(self): | ||||||
|         """ Authorises a Twitter account. This function needs to be called for each new session, after self.get_configuration() and before self.login()""" |         """ Authorises a Twitter account. This function needs to be called for each new session, after self.get_configuration() and before self.login()""" | ||||||
|         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: |         auth = tweepy.OAuth1UserHandler(appkeys.twitter_api_key, appkeys.twitter_api_secret) | ||||||
|             self.auth = tweepy.OAuth1UserHandler(appkeys.twitter_api_key, appkeys.twitter_api_secret) |         redirect_url = auth.get_authorization_url() | ||||||
|             redirect_url = self.auth.get_authorization_url() |         webbrowser.open_new_tab(redirect_url) | ||||||
|             webbrowser.open_new_tab(redirect_url) |         verification_dialog = wx.TextEntryDialog(None, _("Enter your PIN code here"), _("Authorising account...")) | ||||||
|             self.authorisation_dialog = authorisationDialog() |         answer = verification_dialog.ShowModal() | ||||||
|             self.authorisation_dialog.cancel.Bind(wx.EVT_BUTTON, self.authorisation_cancelled) |         code = verification_dialog.GetValue() | ||||||
|             self.authorisation_dialog.ok.Bind(wx.EVT_BUTTON, self.authorisation_accepted) |         verification_dialog.Destroy() | ||||||
|             self.authorisation_dialog.ShowModal() |         if answer != wx.ID_OK: | ||||||
|  |             return | ||||||
|     def verify_authorisation(self, pincode): |         try: | ||||||
|         self.auth.get_access_token(pincode) |             auth.get_access_token(code) | ||||||
|         self.settings["twitter"]["user_key"] = self.auth.access_token |         except TweepyException: | ||||||
|         self.settings["twitter"]["user_secret"] = self.auth.access_token_secret |             dlg = wx.MessageDialog(None, _("We could not authorice your Twitter account to be used in TWBlue. This might be caused due to an incorrect verification code. Please try to add the session again."), _("Authorization error"), wx.ICON_ERROR) | ||||||
|  |             dlg.ShowModal() | ||||||
|  |             dlg.Destroy() | ||||||
|  |             return False | ||||||
|  |         self.create_session_folder() | ||||||
|  |         self.get_configuration() | ||||||
|  |         self.settings["twitter"]["user_key"] = auth.access_token | ||||||
|  |         self.settings["twitter"]["user_secret"] = auth.access_token_secret | ||||||
|         self.settings.write() |         self.settings.write() | ||||||
|         del self.auth |         return True | ||||||
|  |  | ||||||
|     def authorisation_cancelled(self, *args, **kwargs): |  | ||||||
|         """ Destroy the authorization dialog. """ |  | ||||||
|         self.authorisation_dialog.Destroy() |  | ||||||
|         del self.authorisation_dialog  |  | ||||||
|  |  | ||||||
|     def authorisation_accepted(self, *args, **kwargs): |  | ||||||
|         """ Gets the PIN code entered by user and validate it through Twitter.""" |  | ||||||
|         pincode = self.authorisation_dialog.text.GetValue() |  | ||||||
|         self.verify_authorisation(pincode) |  | ||||||
|         self.authorisation_dialog.Destroy() |  | ||||||
|  |  | ||||||
|     def api_call(self, call_name, action="", _sound=None, report_success=False, report_failure=True, preexec_message="", *args, **kwargs): |     def api_call(self, call_name, action="", _sound=None, report_success=False, report_failure=True, preexec_message="", *args, **kwargs): | ||||||
|         """ Make a call to the Twitter API. If there is a connectionError or another exception not related to Twitter, It will call the method again at least 25 times, waiting a while between calls. Useful for  post methods. |         """ Make a call to the Twitter API. If there is a connectionError or another exception not related to Twitter, It will call the method again at least 25 times, waiting a while between calls. Useful for  post methods. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user