Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Token example does not work. #43

Open
sjorge opened this issue Jan 28, 2024 · 7 comments
Open

API Token example does not work. #43

sjorge opened this issue Jan 28, 2024 · 7 comments

Comments

@sjorge
Copy link

sjorge commented Jan 28, 2024

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
Copy link
Author

sjorge commented Jan 28, 2024

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'

@Sean-Brooks
Copy link

@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

@s-t-e-v-e-n-k
Copy link
Contributor

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.

@sjorge
Copy link
Author

sjorge commented Feb 22, 2024

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)

@xenago
Copy link

xenago commented Feb 25, 2024

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!

@Touchstone64
Copy link

@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?

@s-t-e-v-e-n-k
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants