jellyfin-apiclient-python icon indicating copy to clipboard operation
jellyfin-apiclient-python copied to clipboard

API Token example does not work.

Open sjorge opened this issue 1 year ago • 7 comments

There is a missing " on this line:

client.authenticate({"Servers": [{"AccessToken: <API key here>, "address": <Server Address>}]}, discover=False)

It should be:

client.authenticate({"Servers": [{"AccessToken": <API key here>, "address": <Server Address>}]}, discover=False)

Although after adding that, there is now an error about missing key DateLastAccessed.

Traceback (most recent call last):
  File "/home/sjorge/anilist-jf-sync/./sync.py", line 29, in <module>
    jas = JellyfinAnilistSync(
  File "/home/sjorge/anilist-jf-sync/./sync.py", line 25, in __init__
    self.client.authenticate({"Servers": [{"AccessToken": api, "address": url}]}, discover=False)
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/client.py", line 55, in authenticate
    state = self.auth.connect(options or {}, discover)
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/connection_manager.py", line 197, in connect
    servers = self.get_available_servers(discover)
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/connection_manager.py", line 88, in get_available_servers
    servers.sort(key=itemgetter('DateLastAccessed'), reverse=True)
KeyError: 'DateLastAccessed'

sjorge avatar Jan 28 '24 13:01 sjorge

I tried with:

client.authenticate({"Servers": [{"AccessToken": api, "address": url, "DateLastAccessed": 0}]}, discover=False)

But that just results in different errors:

Traceback (most recent call last):
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/connection_manager.py", line 176, in connect_to_server
    result = self.API.get_public_info(server.get('address'))
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/api.py", line 585, in get_public_info
    response = self.send_request(server_address, "system/info/public")
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/api.py", line 532, in send_request
    "headers": headers or self.get_default_headers(),
  File "/home/sjorge/anilist-jf-sync/venv/lib/python3.10/site-packages/jellyfin_apiclient_python/api.py", line 513, in get_default_headers
    auth += "Device=%s, " % self.config.data['app.device_name']
KeyError: 'app.device_name'

Failing server connection. ERROR msg: 'app.device_name'

sjorge avatar Jan 28 '24 13:01 sjorge

@sjorge I was having the same issue. Using your method above, I was able to connect by instantiating the .app() info, and passing DateLastAccessed and UserId through .authenticate() with a GET request. You'll need to supply your username in the function below.

import requests
from jellyfin_apiclient_python import JellyfinClient

def get_user_id():
    url = f"{JELLYFIN_HOST}/Users"
    headers = {
        'Accept': 'application/json',
        'X-Emby-Token': API_TOKEN}
    user_data = requests.request("GET", url, headers=headers, verify=False)
    for user in user_data.json():
        if '<your_user_name>' in user['Name']:
            return user['Id']

client = JellyfinClient()
client.config.app('jellyfin', '0.0.1', 'machine_name', 'unique_id')
client.config.data["auth.ssl"] = True
client.authenticate(
    {"Servers": [{"AccessToken": API_TOKEN, "address": JELLYFIN_HOST, "DateLastAccessed": 0, "UserId": get_user_id()}]},
    discover=True)

>>> client.logged_in
True

Hopefully this helps

Sean-Brooks avatar Feb 15 '24 03:02 Sean-Brooks

client.config.app('jellyfin', '0.0.1', 'machine_name', 'unique_id')

@Sean-Brooks This also means you are not authenticating with an API token, but a device access token -- the differences here are annoying, but it's due to the server overloading what the meaning of AccessToken means.

@sjorge I've pushed a PR to fix the missing quote, but the missing app.device_name means the code also thinks you're using a device access token rather than an API key. I'd need to see your code to work out what is going wrong.

s-t-e-v-e-n-k avatar Feb 22 '24 06:02 s-t-e-v-e-n-k

I gave up and ended up just doing it manually with the requests library.

Although jellyfin server doesn't really seem to like just plain API token access as it shows some undefined around in the dashboard. (But it's good enough not to have to show up as a full client or authenticate as a user)

But looking at my notes it immediately throws that after doing

client.authenticate({"Servers": [{"AccessToken": api, "address": url}]}, discover=False)

sjorge avatar Feb 22 '24 06:02 sjorge

Yeah using an API key doesn't work with this package. After fixing the broken syntax in the example, I see the same error with jellyfin_apiclient_python/connection_manager.py:88.

Plain requests works fine though!

xenago avatar Feb 25 '24 02:02 xenago

@s-t-e-v-e-n-k PyPI reports 1.9.2 (September 2022) as the latest release of the package, could this explain why users are reporting the behaviour from before your commit was merged?

Touchstone64 avatar Mar 23 '24 14:03 Touchstone64

@Touchstone64 It's a good point, people might need to install from git.

s-t-e-v-e-n-k avatar Mar 24 '24 10:03 s-t-e-v-e-n-k