[How To] Request a X-Plex-Token token for your app

other-dev

#1

Every client must request its own X-Plex-Token and register with plex.tv. This requires a valid Plex account username and password.

Do a HTTP POST request to https://plex.tv/users/sign_in.json (or https://plex.tv/users/sign_in.xml) with:

  • user[login] and user[password] form values in the body. For example: user%5Blogin%5D=plexusername&user%5Bpassword%5D=myplexpassword

  • HTTP header: X-Plex-Client-Identifier: a unique value for your client

  • HTTP header: X-Plex-Product: the name of your client

  • HTTP header: X-Plex-Version: the version of your client

Upon success you will get back a JSON (or XML) formatted response that contains a token you can use with other requests to the Plex Media Server. This token must be sent as a HTTP header (X-Plex-Token).


Your registered client will be listed under "Devices" (see attachment).

(Edit: Updated recommended method from using Basic Auth to passing the 'user[login]' and 'user[password]' parameters.)


#2

Here is an example that works in Powershell - for those of us using it :P

---------------------------------------------------------------------------------------------------------

clear

$url = "https://plex.tv/users/sign_in.xml"

$BB = [System.Text.Encoding]::UTF8.GetBytes("myplexaccount:mypassword")

$EncodedPassword = [System.Convert]::ToBase64String($BB)

$headers = @{}

$headers.Add("Authorization","Basic $($EncodedPassword)") | out-null

$headers.Add("X-Plex-Client-Identifier","TESTSCRIPTV1") | Out-Null

$headers.Add("X-Plex-Product","Test script") | Out-Null

$headers.Add("X-Plex-Version","V1") | Out-Null

[xml]$res = Invoke-RestMethod -Headers:$headers -Method Post -Uri:$url

$token = $res.user.authenticationtoken

---------------------------------------------------------------------------

Maybe their is a better place to put this, but, this seemed like a good one to store examples to help others along.

Cade


#3

This is detailed and outlined In the API sticky here: https://forums.plex.tv/topic/104353-pms-web-api-documentation/

The google wiki is here: link is in the OP of the pms web api documentation.

https://code.google.com/p/plex-api/w/list

Along with other API documentation.


#4

Is it mandatory now that accounts and rights are handled in PMS directly and no more on website ?

Is the token returned is always the same for a specific account ?

Thanks


#5

Is it mandatory now that accounts and rights are handled in PMS directly and no more on website ?

 
Not sure what you mean with "on the website", but yes, every piece of software what wants to communicate with PMS has to get its own token.
 

Is the token returned is always the same for a specific account ?


 

The token you get back from myPlex is different every time you do a request for a token. However the token doesn't expire.


#6

The token you get back from myPlex is different every time you do a request for a token. However the token doesn't expire.

Thanks for confirmation, now I need to find how to send GET requests correctly as URL I used in past are no more workirg (404 all the time) :(


#7

I'm confused how Plex Home uses tokens to authenticate users and I can't get authentication for all users to work. 

Using the following Python script I am able to use the username and password of a Plex user to obtain a token.

import httplib, urllib, base64
username = "insertanyusernamehere"
password = "insertcorrespondingpasswdhere"
base64string = base64.encodestring('%s:%s' % (username, password)).replace('
', '')
txdata = ""

headers={'Authorization': "Basic %s" % base64string,
                'X-Plex-Client-Identifier': "Test script",
                'X-Plex-Product': "Test script 356546545",
                'X-Plex-Version': "0.001"}

conn = httplib.HTTPSConnection("plex.tv")
conn.request("POST","/users/sign_in.json",txdata,headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print str(data)
conn.close()

The script returns some user info, including a token. Next, I try to use the token to access the Plex API with the following script:

import httplib, urllib, base64

token = "INSERTTOKENHERE"

txdata = ""

headers={'X-Plex-Client-Identifier': "Test script",
                'X-Plex-Product': "Test script 356546545",
                'X-Plex-Version': "0.001",
                'X-Plex-Token': token}

conn = httplib.HTTPConnection(":32400")
conn.request("GET","/library",txdata,headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print str(data)
conn.close()

When I enter the account credentials of the server's owner in the first script and insert the corresponding token into the second script, I get some info about the libraries on the Plex server, as expected. However, when I enter the credentials of any other Home user the first script returns a token successfully, but the second script returns a 401 Unauthorized error.  

Can someone tell me what I've been doing wrong?


#8
Hallo,
i have error 422 in javascript:

GET https://plex.tv/users/sign_in.xml 422 (Unprocessable Entity)

GET https://plex.tv/devices.xml works and plex.tv response my clients....


#9

i have error 422 in javascript:

GET https://plex.tv/users/sign_in.xml422 (Unprocessable Entity)

You have to use POST instead of GET for this request, with the required POST data (see first post in this thread).


#10

You have to use POST instead of GET for this request, with the required POST data (see first post in this thread).

Thank You


#11

hey. is there any solution to pypass the plex-tokens ?
i wait for 8 months now, for an app to include myPlex-Tokens .. but seems that they didn't think its imporant. Plex Home broke any possibility for me to use Plex and this app , since the introduction of plex home. And i think that there are more apps out there with the same problem. So please add the ability to disable the requirement for authentication INSIDE MY OWN NETWORK, please. I mean, for what is that option, if not available with plex home...


#12

@snickers said:
hey. is there any solution to pypass the plex-tokens ?
i wait for 8 months now, for an app to include myPlex-Tokens .. but seems that they didn't think its imporant. Plex Home broke any possibility for me to use Plex and this app , since the introduction of plex home. And i think that there are more apps out there with the same problem. So please add the ability to disable the requirement for authentication INSIDE MY OWN NETWORK, please. I mean, for what is that option, if not available with plex home...

This really isn't the thread to raise these concerns. You should be posting on the thread for whatever app you are using that hasn't been updated yet to properly handle using token based auth. Or starting your own thread to raise your request to allow bypassing authentication with Plex Home enabled, but I can tell you right now that will be rejected as that allows bypassing the content restrictions Plex Home supports, which is one of the primary reasons for adopting it in the first place.


#13

This is an example for get Token in PHP

$host = "https://plex.tv/users/sign_in.json";
$token = curl_init($host);
curl_setopt($token, CURLOPT_USERPWD, "USER:PASSWORD\$");
curl_setopt($token, CURLOPT_HTTPHEADER, array('X-Plex-Client-Identifier: 7608cf36-742b-11e4-8b39-00089bd210a2', 'X-Plex-Device: Ubuntu', 'X-Plex-Device-Name: OptiPlex', 'X-Plex-Product: plexWatchWeb', 'X-Plex-Version: 1.0'));
curl_setopt($token, CURLOPT_TIMEOUT, 30);
curl_setopt($token, CURLOPT_POST, 1);
curl_setopt($token, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($token);
$arreglo_json = json_decode($json, true);
curl_close($token);


#14

Your X-Plex-Client-Identifier should be unique per client, so you would want to generate that with something like uniqid() in PHP.


#15

Thanks finally figured it out!


#16

@constantinople11 said:
According to https://github.com/Arcanemagus/plex-api/wiki/Plex.tv and https://code.google.com/p/plex-api/wiki/PlexTv, This URL https://plex.tv/servers.xml should ----> Gets a list of servers and their sections

The correct URL is https://plex.tv/pms/servers.xml, with /pms/ in the middle of the URL.


#17

@sander1 said:
The correct URL is https://plex.tv/pms/servers.xml, with /pms/ in the middle of the URL.

A related query is there a known method to authorize the new user pin feature, will be easy to get the pin from the uer but how and which url will validate the pin, I did a search in the forum but cannot seem to find any api.

Found this https://gist.github.com/suderman/e01b73080f29fef9d241 but am stull unsure.

/iSh0w


#18

@sander1 said:
The correct URL is https://plex.tv/pms/servers.xml, with /pms/ in the middle of the URL.

Thank you! It worked fine!


#19

Thanks finally figured it out!


#20

@sander1 said:
The correct URL is https://plex.tv/pms/servers.xml, with /pms/ in the middle of the URL.

Updated the Wiki, in the future when you see issues like that please fix them ;)
That wiki is just a copy of the old Google Code version, with a few bugs I've ran into already fixed. There is quite a bit of room for improvement!