Help with extension auth

i’ve got a roon extension that was authorized … i did some rework and removed the authorization …when i run the service i’m not getting an auth request to approve … just waiting for approval but i never see it in the app … any ideas what my be wrong i rebooted nucleus

token = None
if os.path.exists(TOKEN_FILE):
try:
with open(TOKEN_FILE, “r”) as f:
token = f.read().strip()
except Exception as read_err:
logger.warning(f"Failed to read {TOKEN_FILE}: {read_err}")

try:
    # Discover Core or fallback to static IP
    host, port = ROON_HOST, ROON_PORT
    try:
        discover = RoonDiscovery(None)
        server = discover.first()
        discover.stop()
        if server:
            host, port = server[0], server[1]
    except Exception as disc_err:
        logger.debug(f"RoonDiscovery fallback to static IP: {disc_err}")

    # Instantiate API client
    roon_instance = RoonApi(APP_INFO, token=token, host=host, port=port)

    # Save token once approved/refreshed
    if roon_instance.token and roon_instance.token != token:
        with open(TOKEN_FILE, "w") as f:
            f.write(str(roon_instance.token))
        logger.info(f"Token successfully written to {TOKEN_FILE}")

    roon = roon_instance
    return roon

This is really best in the Tinkering > Roon API section

The most likely cause is the stale token. Your code still reads TOKEN_FILE and passes the old token to RoonApi. When the Core gets a token it has revoked, it often just leaves the extension in limbo instead of re-listing it as a new request.

Try this, it is a cleaned-up version that deletes bad tokens, logs where it’s connecting, and waits for approval before saving the token:

import os, time
from roonapi import RoonApi, RoonDiscovery

def connect_roon():
    global roon

    # Load token, ignoring junk values
    token = None
    if os.path.exists(TOKEN_FILE):
        try:
            with open(TOKEN_FILE) as f:
                t = f.read().strip()
            if t and t != "None":
                token = t
        except Exception as e:
            logger.warning(f"Failed to read {TOKEN_FILE}: {e}")

    # Discover Core, fall back to static
    host, port = ROON_HOST, ROON_PORT  # ROON_PORT should be 9330 for Roon 2.x
    try:
        discover = RoonDiscovery(None)
        server = discover.first()
        discover.stop()
        if server:
            host, port = server[0], server[1]
    except Exception as e:
        logger.debug(f"Discovery failed, using static: {e}")

    logger.info(f"Connecting to Roon Core at {host}:{port} "
                f"({'with' if token else 'without'} token)")

    roon_instance = RoonApi(APP_INFO, token, host, port, blocking_init=False)

    # Wait for approval (Settings → Extensions → Enable)
    logger.info("Waiting for authorisation in Roon > Settings > Extensions...")
    while not roon_instance.token:
        time.sleep(1)

    # Save token once we have it
    if roon_instance.token != token:
        with open(TOKEN_FILE, "w") as f:
            f.write(roon_instance.token)
        logger.info(f"Token written to {TOKEN_FILE}")

    roon = roon_instance
    return roon

Moved (10chars)

1 Like

Just in case it’s helpful I found that some roon clients (in my case a Mac) would respond to a discovery packet.

In any case it’s possible to have more that one roon server on the network. So you can end up asking the wrong server for authorisation (and never getting it!).

My fix is to ask all the discovered servers for authorisation - and stop when one of them gets a token.

This is in python (using my library) - but the logic could be the same.

discover = RoonDiscovery(None)
servers = discover.all()

print("Shutdown discovery")
discover.stop()

print("Found the following servers")
print(servers)
apis = [RoonApi(appinfo, None, server[0], server[1], False) for server in servers]

auth_api = []
while len(auth_api) == 0:
    print("Waiting for authorisation")
    time.sleep(1)
    auth_api = [api for api in apis if api.token is not None]
api = auth_api[0]
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.