Last active
December 20, 2025 05:10
-
-
Save mcarlton00/f7bd7218828ed465ce0f309cebf9a247 to your computer and use it in GitHub Desktop.
Example for authenticating to Jellyfin API from Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| # Define connection details | |
| server_url = 'http://192.168.0.200:8096' | |
| username = 'User' | |
| password = 'Password' | |
| # Build json payload with auth data | |
| auth_data = { | |
| 'username': username, | |
| 'Pw': password | |
| } | |
| headers = {} | |
| # Build required connection headers | |
| authorization = 'MediaBrowser Client="other", Device="my-script", DeviceId="some-unique-id", Version="0.0.0"' | |
| headers['Authorization'] = authorization | |
| # Authenticate to server | |
| r = requests.post(server_url + '/Users/AuthenticateByName', headers=headers, json=auth_data) | |
| # Retrieve auth token and user id from returned data | |
| token = r.json().get('AccessToken') | |
| user_id = r.json().get('User').get('Id') | |
| # Update the headers to include the auth token | |
| headers['Authorization'] = f'{authorization}, Token="{token}"' | |
| # Requests can be made with | |
| #requests.get(f'{server_url}/api/endpoint', headers=headers) |
No, it was too difficult. The login was a better option for my project to begin with
Thanks! I was trying to use the api key but eventually came back to this.
Did you ever get it to work; With the api key? Although username and password is the simplest, I'd rather not deal with passwords
The auth header for an API key is X-MediaBrowser-Token
Not sure why its not js Authorization but whatever
Hi there,
If you have 2 running instances of jellyfin (like one new, and one old), I (chatGPT) made a script to transfer every favorites from every users
import requests
# Servers A and B
server_a_url = 'http://192.168.0.200:8096'
server_b_url = 'http://192.168.0.201:8096' # The URL of the second server
# Authentication credentials
username = 'User'
password = 'Password'
# Authenticate and retrieve the token
def authenticate(server_url):
auth_data = {
'username': username,
'Pw': password
}
headers = {}
authorization = 'MediaBrowser Client="other", Device="my-script", DeviceId="some-unique-id", Version="0.0.0"'
headers['Authorization'] = authorization
response = requests.post(f'{server_url}/Users/AuthenticateByName', headers=headers, json=auth_data)
if response.status_code == 200:
token = response.json().get('AccessToken')
user_id = response.json().get('User').get('Id')
headers['Authorization'] = f'{authorization}, Token="{token}"'
return headers
else:
raise Exception(f"Authentication failed on {server_url}")
# Get the list of users from a server
def get_users(server_url, headers):
response = requests.get(f'{server_url}/Users', headers=headers)
if response.status_code == 200:
return response.json() # List of users
else:
raise Exception(f"Failed to fetch users from {server_url}")
# Get the favorites of a user
def get_favorites(server_url, user_id, headers):
response = requests.get(f'{server_url}/Users/{user_id}/Items?Recursive=True&Filters=IsFavorite&Fields=Path', headers=headers)
if response.status_code == 200:
return response.json().get("Items") # List of favorites
else:
raise Exception(f"Failed to fetch favorites for user {user_id} from {server_url}")
# Add a favorite to server B
def add_favorite_to_server_b(server_b_url, user_id, item_id, headers):
response = requests.post(f'{server_b_url}/Users/{user_id}/FavoriteItems/{item_id}', headers=headers)
if response.status_code == 200:
print(f"Added item {item_id} to user {user_id}'s favorites on server B")
else:
print(f"Added item {item_id} to user {user_id}'s favorites on server B") # not making a raise Exception for potential missing items
# Main function to transfer favorites
def transfer_favorites():
try:
# Authenticate on both servers
headers_a = authenticate(server_a_url)
headers_b = authenticate(server_b_url)
# Get the list of users from server A
users_a = get_users(server_a_url, headers_a)
# Get the list of users from server B (to check if they exist)
users_b = get_users(server_b_url, headers_b)
# Compare users from both servers
for user_a in users_a:
user_a_id = user_a.get('Id')
user_a_name = user_a.get('Name')
# Check if the user exists on server B
if any(user_b.get('Id') == user_a_id for user_b in users_b):
print(f"Transferring favorites for user {user_a_name} (ID: {user_a_id})")
# Get the favorites of the user from server A
favorites_a = get_favorites(server_a_url, user_a_id, headers_a)
# For each favorite from server A, add it to server B
for item in favorites_a:
item_id = item.get('Id')
add_favorite_to_server_b(server_b_url, user_a_id, item_id, headers_b)
except Exception as e:
print(f"An error occurred: {e}")
# Run the favorite transfer
transfer_favorites()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you ever get it to work; With the api key? Although username and password is the simplest, I'd rather not deal with passwords