Pulling my hair out - Can't get anything API like working

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){

console.log(result);
}
});
}

You mentioned plex home. All requests would need the online token appended to them - there should be some forum topics on the subject

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 :(

Are you saying the information here is out of date? 

https://forums.plex.tv/topic/129922-how-to-request-a-myplex-token-for-your-app/

and reference to it here https://forums.plex.tv/topic/139601-html-api-use-with-plex-home/

SA2000 is correct.....

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')

Then in the web call, add a header as MYHEADER

/T

Thank you both. 

I've made some progress.

I fetch my token from plex.tv then to a post request on my server with these additional headers:

Authorization:Basic 
X-Plex-Client-Identifier:RobsPlexWebClient-1
X-Plex-Device-Name:RobsPlexWebClient-1
X-Plex-Product:RobsPlexWebClient
X-Plex-Token:
X-Plex-Version:1

I'm now getting a response - it's an error - but it's better than me shouting at the screen:

<?xml version="1.0" encoding="UTF-8"?>


email is blank or missing


username is blank or missing


password is blank or missing


I'll go ahead now and try to work out how to sort that.

@dane22 - Do you mind if I ask where you got that code from or how you managed to figure this stuff out?

Thanks for the links - I hadn't seen the second post - I wish I had - much appreciated.

@dane22 - Do you mind if I ask where you got that code from or how you managed to figure this stuff out?

Lots and lots of experimenting, as well as looking @ what's going over the wire

And take a peak @ my Plex2CSV or FindUnmatched source on GitHub for sample code

/T

Thank you both. 

I've made some progress.

I fetch my token from plex.tv then to a post request on my server with these additional headers:

Authorization:Basic 
X-Plex-Client-Identifier:RobsPlexWebClient-1
X-Plex-Device-Name:RobsPlexWebClient-1
X-Plex-Product:RobsPlexWebClient
X-Plex-Token:
X-Plex-Version:1

No need to pass username and pwd or the rest again towards the PMS, the token is enough

/T

Hmm - Strange that it's asking for them. 

I'll have a root around your sources and see if I can work it out. Thanks again :)

Ah...ha. I was doing the same as you said on another thread dan22. Post instead of get. 

I'll be posting my code on github as I scrape this together - here (plex-delete-watched)

Thanks again.