# Getting a token from plex.tv

**URL:** <https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002>\
**Category:** Dev/API Corner\
**Tags:** other-dev\
**Created:** [November 24, 2014, 10:59pm UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002 "2014-11-24T22:59:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 24, 2014, 10:59pm UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/1 "2014-11-24T22:59:54Z")

</div>

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&nbsp; :(

&nbsp;

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&nbsp; :huh:

&nbsp;

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

&nbsp;

/Tommy

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 24, 2014, 11:07pm UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/2 "2014-11-24T23:07:24Z")

</div>

Duh.....Me stupid&nbsp; :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

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 25, 2014, 12:01am UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/3 "2014-11-25T00:01:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [November 25, 2014, 12:03am UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/4 "2014-11-25T00:03:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 25, 2014, 12:12am UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/5 "2014-11-25T00:12:58Z")

</div>

> 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&nbsp; :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&nbsp; :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&nbsp; ;)

Best regards

Tommy

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [November 27, 2014, 3:55pm UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/6 "2014-11-27T15:55:18Z")

</div>

The difference between&nbsp;String.Encode and&nbsp;String.Base64Encode is that with&nbsp;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 \_

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 28, 2014, 6:58pm UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/7 "2014-11-28T18:58:01Z")

</div>

And implemented in [Plex2CSV&nbsp;](https://forums.plex.tv/topic/103115-rel-plex2csv/?p=607323)

Once more huge Kudo to Sander1 for the assistance here

/T

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 21, 2019, 3:39am UTC](https://forums.plex.tv/t/getting-a-token-from-plex-tv/84002/8 "2019-12-21T03:39:50Z")

</div>

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