MCPcopy
hub / github.com/iBaa/PlexConnect / MyPlexSignIn

Function MyPlexSignIn

PlexAPI.py:662–721  ·  view source on GitHub ↗
(username, password, options)

Source from the content-addressed store, hash-verified

660 authtoken - token for subsequent communication with MyPlex
661"""
662def MyPlexSignIn(username, password, options):
663 # MyPlex web address
664 MyPlexHost = 'plex.tv'
665 MyPlexSignInPath = '/users/sign_in.xml'
666 MyPlexURL = 'https://' + MyPlexHost + MyPlexSignInPath
667
668 # create POST request
669 xargs = getXArgsDeviceInfo(options)
670 request = urllib2.Request(MyPlexURL, None, xargs)
671 request.get_method = lambda: 'POST' # turn into 'POST' - done automatically with data!=None. But we don't have data.
672
673 # no certificate, will fail with "401 - Authentification required"
674 """
675 try:
676 f = urllib2.urlopen(request)
677 except urllib2.HTTPError, e:
678 print e.headers
679 print "has WWW_Authenticate:", e.headers.has_key('WWW-Authenticate')
680 print
681 """
682
683 # provide credentials
684 ### optional... when 'realm' is unknown
685 ##passmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
686 ##passmanager.add_password(None, address, username, password) # None: default "realm"
687 passmanager = urllib2.HTTPPasswordMgr()
688 passmanager.add_password(MyPlexHost, MyPlexURL, username, password) # realm = 'plex.tv'
689 authhandler = urllib2.HTTPBasicAuthHandler(passmanager)
690 urlopener = urllib2.build_opener(authhandler)
691
692 # sign in, get MyPlex response
693 try:
694 response = urlopener.open(request).read()
695 except urllib2.HTTPError, e:
696 if e.code==401:
697 dprint(__name__, 0, 'Authentication failed')
698 return ('', '')
699 else:
700 raise
701
702 dprint(__name__, 1, "====== MyPlex sign in XML ======")
703 dprint(__name__, 1, response)
704 dprint(__name__, 1, "====== MyPlex sign in XML finished ======")
705
706 # analyse response
707 XMLTree = etree.ElementTree(etree.fromstring(response))
708
709 el_username = XMLTree.find('username')
710 el_authtoken = XMLTree.find('authentication-token')
711 if el_username is None or \
712 el_authtoken is None:
713 username = ''
714 authtoken = ''
715 dprint(__name__, 0, 'MyPlex Sign In failed')
716 else:
717 username = el_username.text
718 authtoken = el_authtoken.text
719 dprint(__name__, 0, 'MyPlex Sign In successfull')

Callers 1

PlexAPI.pyFile · 0.85

Calls 2

getXArgsDeviceInfoFunction · 0.85
dprintFunction · 0.85

Tested by

no test coverage detected