Jump to content

500 error when creating a collection (via POST /Collections)


Go to solution Solved by Luke,

Recommended Posts

Posted (edited)

Hi all,

I just discovered and started playing around with Emby's API.

As a test, I wrote up a Python script (below) to create a collection of all Classical music.

When I run it, the call to GET /Items works, but then I encounter this 500 error when creating the collection (via POST /Collections):

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

 

I've tried both authentication methods (by username, and API key), but both yield the same result.  Attempting to bypass the error by inserting dashes into any tokens leads to a 401 error.  I wonder if there are other endpoints that have the same issue.

 

 

Python script for reference:

import requests

# Emby server config
EMBY_SERVER = 'http://192.168.0.44:8096'
USERNAME = 'Matt'
PASSWORD = 'xxxxxxxxxxx'
CLASSICAL_GENRE = 'Classical'
COLLECTION_NAME = 'All Classical Music'

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Emby UserId="c2221bf3-e16c-46ef-9cfd-24538fafc96d",Client="Windows", Device="ANUBIS", DeviceId="xxx", Version="1.0.0.0"'
}

def authenticate_user():
    url = f"{EMBY_SERVER}/Users/AuthenticateByName"
    payload = {
        "Username": USERNAME,
        "Pw": PASSWORD
    }
    response = requests.post(url, headers=headers, json=payload)
    response.raise_for_status()
    auth_data = response.json()
    return auth_data['AccessToken'], auth_data['User']['Id']

def get_all_music_items(access_token):
    url = f"{EMBY_SERVER}/Items"
    headers_with_token = {
        **headers,
        'X-Emby-Token': access_token
    }
    params = {
        'IncludeItemTypes': 'Audio',
        'Recursive': 'true',
        'Fields': 'Genres'
    }
    response = requests.get(url, headers=headers_with_token, params=params)
    response.raise_for_status()
    return response.json().get('Items', [])

def filter_classical_music(items):
    return [item for item in items if CLASSICAL_GENRE in item.get('Genres', [])]

def create_collection(access_token, item_ids):
    url = f"{EMBY_SERVER}/Collections"
    headers_with_token = {
        **headers,
        'X-Emby-Token': access_token
    }
    data = {
        'Name': COLLECTION_NAME,
        'Ids': item_ids
    }
    response = requests.post(url, headers=headers_with_token, json=data)
    response.raise_for_status()
    return response.json()

def main():
    print("Authenticating user...")
    access_token, user_id = authenticate_user()

    print("Fetching all music items...")
    all_items = get_all_music_items(access_token)
    classical_items = filter_classical_music(all_items)
    print(f"Found {len(classical_items)} classical music items.")

    if not classical_items:
        print("No classical music items found. Exiting.")
        return

    item_ids = [item['Id'] for item in classical_items]
    print("Creating collection...")
    result = create_collection(access_token, item_ids)
    print("Collection created:", result.get('Name'))

if __name__ == '__main__':
    main()

 

Edited by mrmatt
Fix typo
Posted

Forgot to note: I am running Emby Server 4.8.11.0 on a Raspberry Pi 4 (Raspbian Bullseye).

  • Solution
Posted

Hi, have you tried using the browser debugger to compare your request to what the web app sends for the same action?

  • Thanks 1
Posted

Thanks, it's working now.  I'm an idiot and completely missed that POST /Collections requires query parameters (and not a JSON request body).

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...