Add exception for two-factor required (#1357)

* Add exception for two-factor required

* Update tools/plex-gettoken.py with 2FA exception
This commit is contained in:
JonnyWong16 2024-02-17 14:34:43 -08:00 committed by GitHub
parent 41f6b9cf93
commit 26447d1931
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 3 deletions

View file

@ -29,3 +29,8 @@ class Unsupported(PlexApiException):
class Unauthorized(BadRequest): class Unauthorized(BadRequest):
""" Invalid username/password or token. """ """ Invalid username/password or token. """
pass pass
class TwoFactorRequired(Unauthorized):
""" Two factor authentication required. """
pass

View file

@ -12,7 +12,7 @@ from plexapi import (BASE_HEADERS, CONFIG, TIMEOUT, X_PLEX_ENABLE_FAST_CONNECT,
log, logfilter, utils) log, logfilter, utils)
from plexapi.base import PlexObject from plexapi.base import PlexObject
from plexapi.client import PlexClient from plexapi.client import PlexClient
from plexapi.exceptions import BadRequest, NotFound, Unauthorized from plexapi.exceptions import BadRequest, NotFound, Unauthorized, TwoFactorRequired
from plexapi.library import LibrarySection from plexapi.library import LibrarySection
from plexapi.server import PlexServer from plexapi.server import PlexServer
from plexapi.sonos import PlexSonosClient from plexapi.sonos import PlexSonosClient
@ -237,6 +237,8 @@ class MyPlexAccount(PlexObject):
errtext = response.text.replace('\n', ' ') errtext = response.text.replace('\n', ' ')
message = f'({response.status_code}) {codename}; {response.url} {errtext}' message = f'({response.status_code}) {codename}; {response.url} {errtext}'
if response.status_code == 401: if response.status_code == 401:
if "verification code" in response.text:
raise TwoFactorRequired(message)
raise Unauthorized(message) raise Unauthorized(message)
elif response.status_code == 404: elif response.status_code == 404:
raise NotFound(message) raise NotFound(message)

View file

@ -4,11 +4,16 @@
Plex-GetToken is a simple method to retrieve a Plex account token. Plex-GetToken is a simple method to retrieve a Plex account token.
""" """
from getpass import getpass from getpass import getpass
from plexapi.exceptions import TwoFactorRequired
from plexapi.myplex import MyPlexAccount from plexapi.myplex import MyPlexAccount
username = input("Plex username: ") username = input("Plex username: ")
password = getpass("Plex password: ") password = getpass("Plex password: ")
code = input("Plex 2FA code (leave blank for none): ")
account = MyPlexAccount(username, password, code=code) try:
account = MyPlexAccount(username, password)
except TwoFactorRequired:
code = input("Plex 2FA code: ")
account = MyPlexAccount(username, password, code=code)
print(account.authenticationToken) print(account.authenticationToken)