Plex: announce a stable client identity to plex.tv#4217
Conversation
plexapi defaults the X-Plex-Client-Identifier to the machine's MAC address and the device name to the hostname. Plex binds OAuth tokens to the client identifier, and the MAC address is unstable in containers, so stored tokens could be rejected after a restart. Pin plexapi's process-global identity to Music Assistant's persistent server id and announce 'Music Assistant' as product/device name, both before the OAuth pin login (config flow) and at provider setup. The pin login no longer needs its own headers override, and the provider session reuses the shared PLEX_PRODUCT constant.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR standardizes the Plex client identity sent by plexapi so Plex OAuth tokens remain valid across restarts (especially in containerized environments).
Changes:
- Introduces a process-global Plex identity configuration helper that pins product/device name and client identifier.
- Adds a
PLEX_PRODUCTconstant and replaces hardcoded"Music Assistant"header values with it. - Removes per-call
MyPlexPinLoginheader overrides and relies on the configuredplexapidefaults.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
music_assistant/providers/plex/helpers.py |
Adds configure_plex_identity to set plexapi global identity headers/fields. |
music_assistant/providers/plex/constants.py |
Introduces PLEX_PRODUCT constant for consistent client identity strings. |
music_assistant/providers/plex/__init__.py |
Calls identity configuration before auth/connection; replaces hardcoded header values with PLEX_PRODUCT. |
|
@anatosun if you can resolve the copilot comments we will move this along |
|
Comments have been resolved without any code changes been made or a reasoning why it hasn't been addressed. Only resolve comments if you adjusted the code and fixed the review comment OR you provided reasoning why it was invalid. |
plexapi rebuilds request headers from BASE_HEADERS on every query, so the session-level X-Plex-Client-Identifier was overridden by the global identity (server_id) set in configure_plex_identity() and never reached the wire. Remove the misleading line so the connection identifies as server_id everywhere.
|
I was gaslighted many times by Copilot, so I did not take its comments very seriously, sorry for that. One was actually relevant and fixed in a commit. |
It is right 80% of the time and sometimes its wrong. At least good to see what it flags and either dismiss it (and leave a comment why) or fix it and reply that its fixed. |
|
|
||
| # Identity announced to Plex / plex.tv. Without this, plexapi falls back to the host | ||
| # machine's name (e.g. in the plex.tv "authorized devices" list and during auth). | ||
| PLEX_PRODUCT: Final = "Music Assistant" |
There was a problem hiding this comment.
Might be better to just use the existing APPLICATION_NAME constant from MA constants.py?
| plexapi.X_PLEX_DEVICE_NAME = PLEX_PRODUCT | ||
| plexapi.BASE_HEADERS["X-Plex-Product"] = PLEX_PRODUCT | ||
| plexapi.BASE_HEADERS["X-Plex-Device-Name"] = PLEX_PRODUCT | ||
| if client_id: |
| from music_assistant.mass import MusicAssistant | ||
|
|
||
|
|
||
| def configure_plex_identity(client_id: str) -> None: |
There was a problem hiding this comment.
Because the identity is set in plexapi's process-global state and always with mass.server_id, every Plex provider instance now advertises the same X-Plex-Client-Identifier. Is that OK with multiple instances?
What does this implement/fix?
plexapi defaults the X-Plex-Client-Identifier to the machine's MAC address and the device name to the hostname. Plex binds OAuth tokens to the client identifier, and the MAC address is unstable in containers, so stored tokens could be rejected after a restart.
Pin plexapi's process-global identity to Music Assistant's persistent server id and announce 'Music Assistant' as product/device name, both before the OAuth pin login (config flow) and at provider setup. The pin login no longer needs its own headers override, and the provider session reuses the shared PLEX_PRODUCT constant.
Types of changes
bugfixnew-featureenhancementnew-providerbreaking-changerefactordocumentationmaintenancecidependenciesChecklist
pre-commit run --all-filespasses.pytestpasses, and tests have been added/updated undertests/where applicable.music-assistant/modelsis linked.music-assistant/frontendis linked.