Requesting a new Plex Auth token for Hass.io from the CLI

For those interested, I wanted to setup my Hass.io system to have it’s own Plex Auth token (and not just borrow the Auth token of another browser or device). I figured out a Python3 script, based on an old script by svdlugt that you can run on the CLI that will register your Hass.io system as a separate device and generate a new Auth token that you can use in your Plex integration.

Note that to run this script, you will need to make sure Python3 is available for the CLI with the following command:

`apk add --no-cache python3`

Here is my sample script, which I named “plex_auth_test.py” and made executable on the CLI. Update with your Plex credentials and capture the output to find your shiny new Auth token.

import http.client, urllib.request, urllib.parse, urllib.error, base64
username = "myplexusername"
password = "mysupersecretpassword"

string = '%s:%s' % (username, password)
base64string = base64.standard_b64encode(string.encode('utf-8'))

txdata = ""

headers={'Authorization': "Basic %s" % base64string.decode('utf-8'),
'X-Plex-Client-Identifier': "Hassio",
'X-Plex-Product': "Hass.io System Monitor",
'X-Plex-Version': "0.001"}
conn = http.client.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))

pm

1 Like