Getting a token from plex.tv

Okay, trying to avoid been locked out by the new fancy Home stuff, so thought I would register my little app @ plex.tv, by keep failing here  :(

 

Code is like this:

#********** Get token from plex.tv *********
''' This will get a valid token, that can be used for authenticating if needed, or even better...inserted into the header '''
@route(PREFIX + '/getToken')
def getToken():
	myUrl = 'https://plex.tv/users/sign_in.json'
	userName = 'wiking@upnorth.dk'
	userPwd = 'MyPaSSWord'
	base64string = base64.encodestring('%s:%s' % (userName, userPwd)).replace('
', '')
	MYAUTHHEADER= {}
	MYAUTHHEADER['X-Plex-Product'] = 'TestChannel'
	MYAUTHHEADER['X-Plex-Client-Identifier'] = '7608cf36-742b-11e4-8b39-00089bd210a2'
	MYAUTHHEADER['X-Plex-Version'] = '1.0'
	MYAUTHHEADER['Authorization'] = 'Basic ' + base64string
	httpResponse = HTTP.Request(myUrl, values=None, headers=MYAUTHHEADER, immediate=True)
	print httpResponse.content

But it keeps failing, and logs says:

raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 422: Unprocessable Entity

So what am I missing here  :huh:

 

Please help me...I'm too young to be left out  :D

 

/Tommy

Duh.....Me stupid  :rolleyes:

When not providing a value, I don't post, but do a get, as it seems....

So back on Defcon 1 here, and standing down

/T

And to save others some time, here's the final function:

#********** 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(userName, userPwd):
myUrl = 'https://plex.tv/users/sign_in.json'
base64string = String.Encode('%s:%s' % (userName, userPwd)).replace('
', '')
MYAUTHHEADER= {}
MYAUTHHEADER['X-Plex-Product'] = NAME
MYAUTHHEADER['X-Plex-Client-Identifier'] = APPGUID
MYAUTHHEADER['X-Plex-Version'] = VERSION
MYAUTHHEADER['Authorization'] = 'Basic ' + base64string
DUMMYVALUE = {}
DUMMYVALUE['Barkley'] = 'IsAFineDog'
jsonResponse = JSON.ObjectFromURL(myUrl, headers=MYAUTHHEADER, values = DUMMYVALUE)
# print 'Token is : ' , jsonResponse['user']['authentication_token']
return jsonResponse['user']['authentication_token']

Happy Plexing

Tommy

Hi!

The framework has support for base64 encoding and decoding build in:

base64string = String.Base64Encode('%s:%s' % (userName, userPwd))

You can also 'force' a post without (dummy) post values by setting method='POST' on the HTTP request:

httpResponse = HTTP.Request(myUrl, headers=MYAUTHHEADER, method='POST')

Hi!

The framework has support for base64 encoding and decoding build in:

base64string = String.Base64Encode('%s:%s' % (userName, userPwd))

Cool, but above actually also works, and the sadly old pdf I use states:

String.Encode(s)
Encodes the given string using the framework’s standard encoding (a slight variant on
Base64 which ensures that the string can be safely used as part of a URL).

But guess your call leaves out any doubts, but was sadly, AFAIK, undocumented  :huh:

You can also 'force' a post without (dummy) post values by setting method='POST' on the HTTP request:

httpResponse = HTTP.Request(myUrl, headers=MYAUTHHEADER, method='POST')

Nice, and like the urllib2 version, and better, even though I love sending a "Barkley", he's an overhead, and no Elan....No prune was intended here, so will switch to Sanders methode  :D

But sadly once more undocumented.....SIGH....

From the doc:

JSON.ObjectFromURL(url, values=None, headers={}, cacheTime=None, en-
coding=None, errors=None, timeout=, sleep=0)

Regardless.....Thanks a bunch for the feedback, and feedback to Plex here is that we really need updated docs, so we can produce faster, and not create "barkley" overheads, when hammering on your doorstep for a token  ;)

Best regards

Tommy

The difference between String.Encode and String.Base64Encode is that with String.Encode some characters are replaced with others to make it safe to pass the string as a GET parameter in URLs.

String.Encode replaces:

  • / with @
  • + with *
  • = with _

And implemented in Plex2CSV 

Once more huge Kudo to Sander1 for the assistance here

/T

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.