mirror of
https://github.com/pkkid/python-plexapi
synced 2024-11-24 20:53:09 +00:00
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:
parent
41f6b9cf93
commit
26447d1931
3 changed files with 15 additions and 3 deletions
|
@ -29,3 +29,8 @@ class Unsupported(PlexApiException):
|
|||
class Unauthorized(BadRequest):
|
||||
""" Invalid username/password or token. """
|
||||
pass
|
||||
|
||||
|
||||
class TwoFactorRequired(Unauthorized):
|
||||
""" Two factor authentication required. """
|
||||
pass
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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): ")
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue