Added oauth authorisation to mastodon session

This commit is contained in:
Manuel Cortez 2022-11-16 15:33:27 -06:00
parent 4c5d2b5e04
commit 7835e09c5b
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790

View File

@ -3,6 +3,7 @@ import os
import paths import paths
import time import time
import logging import logging
import webbrowser
import wx import wx
import mastodon import mastodon
import config import config
@ -53,15 +54,26 @@ class Session(base.baseSession):
def authorise(self): def authorise(self):
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: authorisation_dialog = wx.TextEntryDialog(None, _("Please enter your instance URL."), _("Mastodon instance"))
self.authorisation_dialog = authorisationDialog() answer = authorisation_dialog.ShowModal()
answer = self.authorisation_dialog.ShowModal() instance = authorisation_dialog.GetValue()
if answer == wx.ID_OK: authorisation_dialog.Destroy()
client_id, client_secret = mastodon.Mastodon.create_app("TWBlue", api_base_url=self.authorisation_dialog.instance.GetValue(), website="https://twblue.es") if answer != wx.ID_OK:
temporary_api = mastodon.Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=self.authorisation_dialog.instance.GetValue(), mastodon_version=MASTODON_VERSION) return
access_token = temporary_api.log_in(self.authorisation_dialog.email.GetValue(), self.authorisation_dialog.password.GetValue()) client_id, client_secret = mastodon.Mastodon.create_app("TWBlue", api_base_url=authorisation_dialog.GetValue(), website="https://twblue.es")
temporary_api = mastodon.Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=instance, mastodon_version=MASTODON_VERSION)
authorisation_dialog.Destroy()
auth_url = temporary_api.auth_request_url()
webbrowser.open_new_tab(auth_url)
verification_dialog = wx.TextEntryDialog(None, _("Enter the verification code"), _("PIN code authorization"))
answer = verification_dialog.ShowModal()
code = verification_dialog.GetValue()
verification_dialog.Destroy()
if answer != wx.ID_OK:
return
access_token = temporary_api.log_in(code=verification_dialog.GetValue())
self.settings["mastodon"]["access_token"] = access_token self.settings["mastodon"]["access_token"] = access_token
self.settings["mastodon"]["instance"] = self.authorisation_dialog.instance.GetValue() self.settings["mastodon"]["instance"] = instance
self.settings.write() self.settings.write()
def get_user_info(self): def get_user_info(self):