Get Plex token using PlexTV

This commit is contained in:
JonnyWong16 2016-05-15 12:57:03 -07:00
parent 885604ef06
commit 2e1e3f8409
4 changed files with 78 additions and 56 deletions

18
API.md
View file

@ -1037,6 +1037,22 @@ Returns:
``` ```
### get_pms_token
Get the user's Plex token used for PlexPy.
```
Required parameters:
username (str): The Plex.tv username
password (str): The Plex.tv password
Optional parameters:
None
Returns:
string: The Plex token used for PlexPy
```
### get_recently_added ### get_recently_added
Get all items that where recelty added to plex. Get all items that where recelty added to plex.
@ -1094,8 +1110,6 @@ Get the PMS server identifier.
``` ```
Required parameters: Required parameters:
None
hostname (str): 'localhost' or '192.160.0.10' hostname (str): 'localhost' or '192.160.0.10'
port (int): 32400 port (int): 32400

View file

@ -2293,33 +2293,31 @@ $(document).ready(function() {
// Plex.tv auth token fetch // Plex.tv auth token fetch
$("#get-pms-auth-token").click(function() { $("#get-pms-auth-token").click(function() {
$("#pms-token-status").html('<i class="fa fa-refresh fa-spin"></i> Fetching token...'); $("#pms-token-status").html('<i class="fa fa-refresh fa-spin"></i> Fetching token...');
if (($("#pms_username").val() !== '') || ($("#pms_password").val() !== '')) { var pms_username = $("#pms_username").val().trim();
var pms_password = $("#pms_password").val().trim();
if ((pms_username !== '') && (pms_password !== '')) {
$.ajax({ $.ajax({
type: 'POST', type: 'GET',
url: 'https://plex.tv/users/sign_in.xml', url: 'get_pms_token',
dataType: 'xml', data: {
username: pms_username,
password: pms_password
},
cache: false,
async: true, async: true,
headers: {'Content-Type': 'application/xml; charset=utf-8', complete: function(xhr, status) {
'X-Plex-Device-Name': 'PlexPy', var authToken = $.parseJSON(xhr.responseText);
'X-Plex-Product': 'PlexPy', if (authToken) {
'X-Plex-Version': '${common.VERSION_NUMBER}', $("#pms-token-status").html('<i class="fa fa-check"></i> Authentication successful!');
'X-Plex-Platform': '${common.PLATFORM}', $("#pms_token").val(authToken);
'X-Plex-Platform-Version': '${common.PLATFORM_VERSION}', $('#pms-auth-modal').modal('hide');
'X-Plex-Client-Identifier': '${config["pms_uuid"]}', } else {
'Authorization': 'Basic ' + btoa($("#pms_username").val() + ':' + $("#pms_password").val()) $("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> Invalid username or password.');
}, }
error: function(jqXHR, textStatus, errorThrown) {
$("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> Authentication failed!');
},
success: function (xml) {
var authToken = $(xml).find('user').attr('authenticationToken');
$("#pms-token-status").html('<i class="fa fa-check"></i> Authentication successful!');
$("#pms_token").val(authToken);
$('#pms-auth-modal').modal('hide');
} }
}); });
} else { } else {
$("#pms-token-status").html("You must enter both fields."); $("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> Username and password required.');
} }
}); });

View file

@ -415,37 +415,31 @@
$('#pms-token-status').fadeIn('fast'); $('#pms-token-status').fadeIn('fast');
var pms_username = $("#pms_username").val().trim(); var pms_username = $("#pms_username").val().trim();
var pms_password = $("#pms_password").val().trim(); var pms_password = $("#pms_password").val().trim();
if ((pms_username !== '') || (pms_password !== '')) { if ((pms_username !== '') && (pms_password !== '')) {
$.ajax({ $.ajax({
type: "post", type: 'GET',
url: "https://plex.tv/users/sign_in.xml", url: 'get_pms_token',
dataType: 'xml', data: {
username: pms_username,
password: pms_password
},
cache: false,
async: true, async: true,
headers: { complete: function (xhr, status) {
'Content-Type': 'application/xml; charset=utf-8', var authToken = $.parseJSON(xhr.responseText);
'X-Plex-Device-Name': 'PlexPy', if (authToken) {
'X-Plex-Product': 'PlexPy', $("#pms-token-status").html('<i class="fa fa-check"></i> Authentation successful!');
'X-Plex-Version': '${common.VERSION_NUMBER}', $('#pms-token-status').fadeIn('fast');
'X-Plex-Platform': '${common.PLATFORM}', $("#pms_token").val(authToken);
'X-Plex-Platform-Version': '${common.PLATFORM_VERSION}', authenticated = true;
'X-Plex-Client-Identifier': '${config["pms_uuid"]}', getServerOptions(authToken)
'Authorization': 'Basic ' + btoa(pms_username + ':' + pms_password) } else {
}, $("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> Invalid username or password.');
error: function(jqXHR, textStatus, errorThrown) { }
$("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> Authentation failed!');
$('#pms-token-status').fadeIn('fast');
},
success: function (xml) {
var authToken = $(xml).find('user').attr('authenticationToken');
$("#pms-token-status").html('<i class="fa fa-check"></i> Authentation successful!');
$('#pms-token-status').fadeIn('fast');
$("#pms_token").val(authToken);
authenticated = true;
getServerOptions(authToken)
} }
}); });
} else { } else {
$("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> You must enter both fields.'); $("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i> Username and password required.');
$('#pms-token-status').fadeIn('fast'); $('#pms-token-status').fadeIn('fast');
} }
}); });

View file

@ -2738,17 +2738,35 @@ class WebInterface(object):
return return
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out()
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
def get_pms_token(self): @addtoapi()
def get_pms_token(self, username=None, password=None, **kwargs):
""" Get the user's Plex token used for PlexPy.
token = plextv.PlexTV() ```
Required parameters:
username (str): The Plex.tv username
password (str): The Plex.tv password
Optional parameters:
None
Returns:
string: The Plex token used for PlexPy
```
"""
if not username and not password:
return None
token = plextv.PlexTV(username=username, password=password)
result = token.get_token() result = token.get_token()
if result: if result:
return result return result['auth_token']
else: else:
logger.warn(u"Unable to retrieve Plex.tv token.") logger.warn(u"Unable to retrieve Plex.tv token.")
return False return None
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@ -2759,8 +2777,6 @@ class WebInterface(object):
``` ```
Required parameters: Required parameters:
None
hostname (str): 'localhost' or '192.160.0.10' hostname (str): 'localhost' or '192.160.0.10'
port (int): 32400 port (int): 32400