How to acquire permanent plex.tv apikey via cli (not apikey of a plex server)

Hey there,

I know how to acquire an api key to access the api of my plex server. The best way is doing this and then accessing https://plex.tv/pms/servers.xml?X-Plex-Token=[temp_token] to acquire the “permanent” token.

Meaning that that link will supply a token that will expire (they literally say it: “Finding a temporary token is pretty simple”), but the token shown at that endpoint will never expire. So I’d advise to use that permanent token.

Now the question:

How can I acquire the permanent token if I don’t have any server? There are multiple plex.tv api endpoints (so not the api of a plex server, but of the plex.tv database) that I’d like to access with a permanent token but that account doesn’t have access to any server. So I can’t get a server api token that I can use to access the plex.tv api (which is, btw, a bit strange that I can access the plex.tv api with not an plex.tv api key, but a plex server api token).

To make the question simple:
I want to acquire a permanent plex.tv token for an account that doesn’t have access to a server. I have the email and password of that account ofcourse.

I should also note that I want it via cli. I have python-PlexAPI installed and setup, so I could use that if you have a solution that uses that. The perfect solution would be to supply an email and password (aka an plex.tv account), and just get the permanent api key as an answer.

the link you provided, seems to provide the link with the answer you desire, although it doesn’t state ‘permanent’, it is how to auth against plex.

Thank you for replying. I’ve been trying it out, but I’m not succeeding.

First, I started with this:

curl -X POST https://plex.tv/api/v2/pins \
  -H 'accept: application/json' \
  -d 'strong=true' \
  -d 'X-Plex-Product=Plex Test' \
  -d 'X-Plex-Client-Identifier=abcdefg'

And I get the following result:

{
  "id": [[REMOVED]],
  "code": [[REMOVED]],
  "product": "Plex Test",
  "trusted": false,
  "clientIdentifier": "abcdefg",
  "location": {
    [[REMOVED]]
  },
  "expiresIn": 1800,
  "createdAt": "2021-06-17T15:55:59Z",
  "expiresAt": "2021-06-17T16:25:59Z",
  "authToken": null,
  "newRegistration": null
}

Then comes the part where the ‘user’ (that being me), is redirected to a website where he needs to login.

And then I do this:

curl -X GET 'https://plex.tv/api/v2/pins/[[value of "id"]]' \
  -H 'accept: application/json' \
  -d 'code=<pinCode>' \
  -d 'X-Plex-Client-Identifier=<clientIdentifier>'

Ofcourse the value for <pinCode> is missing because, if I understand it correctly, I would get that when I was logging in on that website. However, I need the whole process to work via cli, so redirecting me to a website won’t work…

Is there a way to log in via cli instead of redirecting to a web page? Or is there a way to make an api request where I supply my email and password and where I get the pincode as response?

It is starting to become a bit unclear how the process works.

As far as I know, it is the following:

  1. Make that first api request where I get an id and code value.
  2. Log in with my email (it is done directly using google’s Continue with google option on the login screen), but that also needs to be done via cli. This will validate the code.
  3. Make the second api request with the id, the now validated code and client Identifier.
  4. Recieve api token
  5. After some time, use the first api request listed in the link you gave to check if the token is still valid.
  6. If it’s not, do step 1-4.

And I want it to all be in an automated script so that I can run it, and it will output the api token that I can use.

I don’t think you can accomplish what you wish to achieve, the automated method to get the token.

I do believe that once you follow the process, the token you receive would not normally expire unless it went unused for whatever idle period, or it was otherwise invalidated (ie by changing your account credentials and logging out all sessions).

Unfortunately though, I am not that familiar with it to help in greater detail.

Hopefully someone with more experience can poke in and provide any further insight or help.

If you are able, you might want to check out other 3rd party plex api projects, like tautulli or PlexTraktSync for how they implemented their auth.

python-plexapi has built-in helper functions to do all of the steps in the Authenticating with Plex post for you automatically: plexapi.utils.getMyPlexAccount and plexapi.utils.createMyPlexDevice.

This is how you would use it.

1 Like

I got it:

curl 'https://plex.tv/api/v2/users/signin?X-Plex-Product=Plex%20test&X-Plex-Client-Identifier=abcdefg' -H 'Accept: application/json' --data-raw 'login=[[EMAIL]]&password=[[PASSWORD]]&rememberMe=true'

That’s it. I get a big response where there’s one key named authToken. I don’t know if it’s permanent. We’ll see. I’ll see what the url is when I want to sign in via google, but I don’t have time for that now.

It’s interresting that I was able to do it using one url that you can just curl, instead of the documented (and a bit unclear) 3-4 urls including a web-page (aka not cli friendly).

4 Likes

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