Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

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

sander1sander1 Channel Developer/AdminPosts: 3,571 Plex Ninja

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

«1

Comments

  • ccarvell-plexccarvell-plex Posts: 2 Plex Pass
    edited December 2014

    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

  • JimBurlisonJimBurlison Plex Notify Creator Posts: 902 Plex Pass
    edited August 2015

    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.

  • vincenvincen Posts: 606 Plex Pass

    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

  • sander1sander1 Channel Developer/Admin Posts: 3,571 Plex Ninja
    vincen wrote on January 18 2015, 3:37 PM: »

    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.
     
    vincen wrote on January 18 2015, 3:37 PM: »

    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.
  • vincenvincen Posts: 606 Plex Pass
    sander1 wrote on January 18 2015, 10:09 PM: »

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

  • svdlugtsvdlugt Posts: 28 Plex Pass

    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('\n', '')
    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("<local ip address>: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?

  • sin-xclusivesin-xclusive Posts: 16 Plex Pass
    edited July 2015
    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....

    <Device name="Plex Web (Safari)" publicAddress="xxx.xxx.xxx.xxx" product="Plex Web" productVersion="2.4.3" platform="Safari" platformVersion="8.0" device="OSX" model="hosted" vendor="" provides="" clientIdentifier="xxxxxxxxxxxxxxxx" version="2.4.3" id="13263103" token="xxxxxxxxxxxxxxxx" createdAt="xxxxxxxxxxxxxxxx" lastSeenAt="xxxxxxxxxxxxxxxx" screenResolution="" screenDensity="">
    </Device>
  • sander1sander1 Channel Developer/Admin Posts: 3,571 Plex Ninja
    sin-xclusive wrote on May 5 2015, 6:03 PM: »

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

  • sin-xclusivesin-xclusive Posts: 16 Plex Pass
    sander1 wrote on May 5 2015, 10:08 PM: »

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

    Thank You

  • snickerssnickers Posts: 582 Plex Pass
    edited July 2015

    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...

  • ArcanemagusArcanemagus Posts: 143 Plex Pass

    @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.

  • JC AvilaJC Avila Posts: 29 Plex Pass

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

  • ArcanemagusArcanemagus Posts: 143 Plex Pass

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

  • tomatosmoothietomatosmoothie Posts: 78 ✭✭
    edited August 2015

    Thanks finally figured it out!

  • sander1sander1 Channel Developer/Admin Posts: 3,571 Plex Ninja
    edited August 2015

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

  • L0ck for PlexL0ck for Plex Posts: 278 ✭✭
    edited August 2015

    @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

  • tomatosmoothietomatosmoothie Posts: 78 ✭✭
    edited August 2015

    @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!

  • tomatosmoothietomatosmoothie Posts: 78 ✭✭
    edited August 2015

    Thanks finally figured it out!

  • ArcanemagusArcanemagus Posts: 143 Plex Pass

    @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!

  • jmperjmper Posts: 269 Plex Pass

    Hello

    Could some one help me with authorization?
    I trying using next code but it always get me empty result with error 400
    what I'm doing wrong?

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

    $base64 = base64encode("user:password");

    $header = array();
    $header[] = "Authorization: Basic $base64";
    $header[] = "X-Plex-Client-Identifier: 1234-5678-9012-345";
    $header[] = "X-Plex-Product: my_apps";
    $header[] = "X-Plex-Version: my_app_v1";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($ch);
    curl_close($ch);

«1
Sign In or Register to comment.