I'm trying to write a simple bit of code to list watched tv shows for TWO plex home users on the same server and compare the list to see what is 'safe' to be deleted.
I've tried using php, js, python and I'm constantly hitting brick walls.
The API docs assume a higher level of API knowledge than I'm used to.
At this stage all I'm trying to do is, somehow, access a section of tv shows.
I'm able to, using an ajax call, get an auth-token from plex.tv/users/sign_in.json no problem. I can't understand how to pass that auth-code to my plex server. Its on the same network and I've tried sending it just about every combination of headers known to man but it always just returns a 401 unauthorized.
Example Code:
function doSomething(){
.ajax({
headers:{
'Authorization' : 'Basic '+(’#authcode’).val(),
‘X-Plex-Client-Identifier’ : ‘RobsPlexWebClient-1’, // I made this up
‘X-Plex-Product’ : ‘RobsPlexWebClient’, // I made this up.
‘X-Plex-Version’ : ‘1’
},
type: ‘get’,
url: ‘http://192.168.0.14:32400/myplex/account’,
success: function(result){
Yup - You would think ... sadly all the information (and I've tried to digest A LOT of out-of-date information on here) has been confusing and/or didn't work.
I've tried adding an Authentication header to the http request to my server but it doesn't do anything. I've tried passing in the token I got from plex.tv/users/login.json as well as my base64 encoded username and password but .. nothing :(
You'll need to create a token first @ plex.tv, and then use that in the header instead of auth.
#********** Get token from plex.tv *********
''' This will return a valid token, that can be used for authenticating if needed, to be inserted into the header '''
# DO NOT APPEND THE TOKEN TO THE URL...IT MIGHT BE LOGGED....INSERT INTO THE HEADER INSTEAD
@route(PREFIX + '/getToken')
def getToken():
Log.Debug('Starting to get the token')
if Prefs['Authenticate']:
Log.Debug('Need to generate a token first from plex.tv')
userName = Prefs['Plex_User']
userPwd = Prefs['Plex_Pwd']
myUrl = 'https://plex.tv/users/sign_in.json'
# Create the authentication string
base64string = String.Base64Encode('%s:%s' % (userName, userPwd))
# Create the header
MYAUTHHEADER= {}
MYAUTHHEADER['X-Plex-Product'] = DESCRIPTION
MYAUTHHEADER['X-Plex-Client-Identifier'] = APPGUID
MYAUTHHEADER['X-Plex-Version'] = VERSION
MYAUTHHEADER['Authorization'] = 'Basic ' + base64string
MYAUTHHEADER['X-Plex-Device-Name'] = NAME
# Send the request
try:
httpResponse = HTTP.Request(myUrl, headers=MYAUTHHEADER, method='POST')
myToken = JSON.ObjectFromString(httpResponse.content)['user']['authentication_token']
Log.Debug('Response from plex.tv was : %s' %(httpResponse.headers["status"]))
except:
Log.Critical('Exception happend when trying to get a token from plex.tv')
Log.Critical('Returned answer was %s' %httpResponse.content)
Log.Critical('Status was: %s' %httpResponse.headers)
global MYHEADER
MYHEADER['X-Plex-Token'] = myToken
else:
Log.Debug('Authentication disabled')