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 all items that where recelty added to plex.
@ -1094,8 +1110,6 @@ Get the PMS server identifier.
```
Required parameters:
None
hostname (str): 'localhost' or '192.160.0.10'
port (int): 32400

View file

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

View file

@ -2738,17 +2738,35 @@ class WebInterface(object):
return
@cherrypy.expose
@cherrypy.tools.json_out()
@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()
if result:
return result
return result['auth_token']
else:
logger.warn(u"Unable to retrieve Plex.tv token.")
return False
return None
@cherrypy.expose
@cherrypy.tools.json_out()
@ -2759,8 +2777,6 @@ class WebInterface(object):
```
Required parameters:
None
hostname (str): 'localhost' or '192.160.0.10'
port (int): 32400