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):
""" Invalid username/password or token. """
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)
from plexapi.base import PlexObject
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.server import PlexServer
from plexapi.sonos import PlexSonosClient
@ -237,6 +237,8 @@ class MyPlexAccount(PlexObject):
errtext = response.text.replace('\n', ' ')
message = f'({response.status_code}) {codename}; {response.url} {errtext}'
if response.status_code == 401:
if "verification code" in response.text:
raise TwoFactorRequired(message)
raise Unauthorized(message)
elif response.status_code == 404:
raise NotFound(message)

View file

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