Add OAuth Device Code login with automatic token refresh#704
Add OAuth Device Code login with automatic token refresh#704einanderson wants to merge 3 commits into
Conversation
The implicit-grant link flow never returns a refresh token, so the
OAuth token has to be regenerated manually every few weeks/months.
This adds Twitch's Device Code Grant flow ("Login (device code)" under
Settings -> Login): the user authorizes once with a short code at
twitch.tv/activate, and the add-on then refreshes the access token
silently via the stored refresh token (on demand in api.Twitch and
proactively from the background service). The existing manual link
flow is kept as a fallback. No client secret is required (public
client, default Client-ID). New strings are English source strings,
translatable via Weblate.
Refs anxdpanic#701, anxdpanic#698
|
Heads-up on a limitation I ran into while using this in production (Kodi 21 / LibreELEC, Raspberry Pi 4): The silent token refresh ( i.e. Twitch requires a I verified end-to-end with a self-registered Public client (Client Type: Public on the Twitch dev console — no secret): device login and the So for the auto-refresh to work out of the box, the device flow needs a public client ID — either ship one for the device flow, or document that users must register their own and set it ( |
A silent refresh is impossible for a confidential client (Twitch replies 'missing client secret'). Detect it, stop retrying, and show a one-time hint to set a public Client-ID; the manual login keeps working. Public client ids are unaffected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Addressed in the latest push (87f22df): the add-on now detects the confidential-client case ( |
|
Hi @einanderson The initial activity was notification that an almost 1 year old PR from @Serph91P had been closed/withdrawn, after seemingly no response from maintainers, which prompted me to follow back to his fork, and found he'd actually built and published an updated version to his custom Kodi Addon repo. His approach is rather more hands-on for the user than yours, but perhaps some cross-pollination of ideas or perhaps re-invention-of-wheel scenarios could be sidestepped ? (I am making a big assumption here that you two aren't already in contact - he seems a rather privacy focused guy, with almost no public communication footprint, so can't really tell if there was any communication about his PR with maintainers/contributors/community users). However it goes with your merge I'm happy to be a guinea-pig and clone/test your PRs 😁 |
|
@einanderson I am not very active these days as life has changed. Are you
interested in attempting maintenance on the addon, and on repo submission
addressing the submission reviews? I would like this addon to continue and
could open access for you. Let me know your comfort level. I am willing to
help.
…On Fri, Jun 5, 2026, 1:48 PM kuraikaze ***@***.***> wrote:
*kuraikaze* left a comment (anxdpanic/plugin.video.twitch#704)
<#704 (comment)>
Hi @einanderson <https://github.com/einanderson>
After forlornly following this repo for a very long while, hoping someone
with knowhow could fix-up the plugin so that the Twitch Enhanced
Broadcasting features would be usable, I now get a flurry of notifications
about two contributors in a couple of days 😲
The initial activity was notification that an almost 1 year old PR from
@Serph91P <https://github.com/Serph91P> had been closed/withdrawn, after
seemingly no response from maintainers, which prompted me to follow back to
his fork, and found he'd actually built and published an updated version to
his custom Kodi Addon repo.
<https://github.com/Serph91P/repository.serph91p>
After installing and completing the additional Twitch Developer
Application/Oauth/cookie-harvesting steps in the README.md
<https://github.com/Serph91P/plugin.video.twitch/blob/main/README.md> I
am at last able to play 1440p streams properly (with audio, at least.
Haven't found a Twitch beta participant using HEVC yet)).
His approach is rather more hands on for the user than yours, but perhaps
some cross-pollination of ideas or perhaps re-invention-of-wheel scenarios
could be sidestepped ? (I am making a *big* assumption here that you two
aren't already in contact - he seems a rather privacy focused guy, with
almost no public communication footprint, so can't really tell if there was
any communication about his PR with maintainers or community users).
However it goes with your merge I'm happy to be a guinea-pig and
clone/test your PRs 😁
—
Reply to this email directly, view it on GitHub
<#704?email_source=notifications&email_token=AEGYYGRBIOTPEXBVMW4RF7T46MBX5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINRTGQYDKOJXGQYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-4634059740>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEGYYGV3EUFTCX4DIY4ROIL46MBX5AVCNFSM6AAAAACZ35U4S2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DMMZUGA2TSNZUGA>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/AEGYYGVHWOXWAGROPWOUAFD46MBX5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINRTGQYDKOJXGQYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ>
and Android
<https://github.com/notifications/mobile/android/AEGYYGU25KUFA5JGURRVOGL46MBX5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINRTGQYDKOJXGQYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ>.
Download it today!
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
|
Repo submission has automated triggers, so that isn't much effort on your
part.
…On Fri, Jun 5, 2026, 2:04 PM Tim Andrews ***@***.***> wrote:
@einanderson I am not very active these days as life has changed. Are you
interested in attempting maintenance on the addon, and on repo submission
addressing the submission reviews? I would like this addon to continue and
could open access for you. Let me know your comfort level. I am willing to
help.
On Fri, Jun 5, 2026, 1:48 PM kuraikaze ***@***.***> wrote:
> *kuraikaze* left a comment (anxdpanic/plugin.video.twitch#704)
> <#704 (comment)>
>
> Hi @einanderson <https://github.com/einanderson>
> After forlornly following this repo for a very long while, hoping someone
> with knowhow could fix-up the plugin so that the Twitch Enhanced
> Broadcasting features would be usable, I now get a flurry of notifications
> about two contributors in a couple of days 😲
>
> The initial activity was notification that an almost 1 year old PR from
> @Serph91P <https://github.com/Serph91P> had been closed/withdrawn, after
> seemingly no response from maintainers, which prompted me to follow back to
> his fork, and found he'd actually built and published an updated version to
> his custom Kodi Addon repo.
> <https://github.com/Serph91P/repository.serph91p>
> After installing and completing the additional Twitch Developer
> Application/Oauth/cookie-harvesting steps in the README.md
> <https://github.com/Serph91P/plugin.video.twitch/blob/main/README.md> I
> am at last able to play 1440p streams properly (with audio, at least.
> Haven't found a Twitch beta participant using HEVC yet)).
>
> His approach is rather more hands on for the user than yours, but perhaps
> some cross-pollination of ideas or perhaps re-invention-of-wheel scenarios
> could be sidestepped ? (I am making a *big* assumption here that you two
> aren't already in contact - he seems a rather privacy focused guy, with
> almost no public communication footprint, so can't really tell if there was
> any communication about his PR with maintainers or community users).
>
> However it goes with your merge I'm happy to be a guinea-pig and
> clone/test your PRs 😁
>
> —
> Reply to this email directly, view it on GitHub
> <#704?email_source=notifications&email_token=AEGYYGRBIOTPEXBVMW4RF7T46MBX5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINRTGQYDKOJXGQYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-4634059740>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AEGYYGV3EUFTCX4DIY4ROIL46MBX5AVCNFSM6AAAAACZ35U4S2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DMMZUGA2TSNZUGA>
> .
> Triage notifications, keep track of coding agent tasks and review pull
> requests on the go with GitHub Mobile for iOS
> <https://github.com/notifications/mobile/ios/AEGYYGVHWOXWAGROPWOUAFD46MBX5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINRTGQYDKOJXGQYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ>
> and Android
> <https://github.com/notifications/mobile/android/AEGYYGU25KUFA5JGURRVOGL46MBX5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINRTGQYDKOJXGQYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ>.
> Download it today!
> You are receiving this because you are subscribed to this thread.Message
> ID: ***@***.***>
>
|
|
at the end i'am a vibe coder and using claude code to find a way to solve the problems. the decision is on your side. It's my first day on git so... :) but i solved in the last few days some issues with only ai support. |
|
@einanderson Cleared all my old settings, used Device Code auth & tried a mix of 1080p_h.264 / 1440p_h.264 / 1440p_h.265 - live & VODs at Source default quality. All streamed succesfully. Notably, audio stream selects/plays correctly and no ads appear, which were the major bugbears with the extant 3.0.2 version & Enhanced Broadcast. One remaining issue I have - which I don't believe you've looked at yet, from the commits ... Not a big deal, and absolutely no pressure from me - I've gotten into the habit of Following channels on PC/Browser so that I can watch them on Kodi/TV from Following Channels list, and that still works fine. I mostly only remembered it was an issue at all because I was clicking around trying to find things to usefully provide feedback on and saw the Search feature which I had previously hidden via settings on old install 😁 |
Thanks, that really means a lot. I can't take on full maintenance right now, but I'd love to stay involved — I'll keep contributing and send PRs every now and then that you can review and merge whenever it suits you. (As I mentioned on the search PR, I work on these with Claude (Anthropic's Claude Code) assisting — just so the expectation's clear.) Appreciate you keeping the project alive, and the offer to help — glad to chip in where I can. |
…cess lock The rotating, single-use refresh token could be lost under Kodi's multi-process settings race: several add-on processes (service + plugin calls) refreshing at once and consuming the same single-use token, or one clobbering another's settings.xml write from its in-memory cache. This surfaced as "Invalid refresh token" and broke the silent auto-refresh after a while. Keep the tokens in addon_data/oauth_tokens.json instead: read fresh on every access, written atomically via os.replace, and guarded by a cross-process fcntl lock around the whole refresh with a re-check inside the lock so a rotated token is never consumed twice. Migrates once from the legacy settings (mirrored back for backward compatibility) and preserves the confidential-client handling. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@anxdpanic i'm doing something to keep this project alive :) Thx for your work! |
Summary
Adds Twitch Device Code Grant login + silent token refresh, so the OAuth token no longer has to be regenerated manually.
Addresses #701 (Using Device Grant Flow) and #698 (OAuth token stops working).
What it does
twitch.tv/activate.refresh_token+ expiry and refreshes the access token automatically (on demand inapi.Twitch.__init__, and proactively in the background service ~every 5 min).missing client secret. In that case the add-on shows a one-time hint and falls back to the manual login — and stops retrying, so there is no log spam. For automatic refresh, register a Public application atdev.twitch.tv/console/appsand set its Client-ID under Settings → Login. (So it may be worth shipping a public Client-ID for the device flow.)Implementation
addon/device_oauth.py— device-code request/poll + refresh (purerequests).routes/device_login.py— interactive login dialog (abort-aware viaxbmc.Monitor).addon/utils.py—ensure_valid_token()+ token storage helpers; gracefully detects a confidential client and disables retrying for it.addon/api.py,service.py— refresh hooks.settings.xml,addon/strings.py,en_gb/strings.po— action + 3 hidden settings (oauth_refresh_token,oauth_token_expiry,oauth_refresh_unsupported) + English source strings (translatable via Weblate).Testing
refresh_tokengrant both succeed without a secret;ensure_valid_token()refreshes cleanly).missing client secret, the add-on shows the hint once and falls back without retry spam.requests+ standardxbmc/xbmcguiAPIs (no OS-specific code) → platform-independent by design; cross-platform testing (Windows/Android/macOS) welcome.Notes
Robust token storage (follow-up)
The rotating, single-use refresh token could be lost under Kodi's multi-process settings race: the background service and plugin invocations can refresh concurrently — consuming the same single-use token — or clobber each other's
settings.xmlwrite from an in-memory cache. That surfaced asInvalid refresh tokenand broke the silent refresh after a while.Tokens are now kept in
addon_data/oauth_tokens.jsoninstead of the Kodi settings:os.replace;fcntllock with a re-check inside the lock, so a rotated token is never consumed twice;Verified by forcing two refresh cycles: the refresh token rotated and stayed valid, with no
Invalid refresh token.