From f2e2d22b372b2b84c570eb9f3042c63429903c3f Mon Sep 17 00:00:00 2001 From: Max Huk Date: Wed, 3 Jun 2026 20:46:15 +0200 Subject: [PATCH] feat(auth): request yt-analytics-monetary scope for revenue metrics The registry already exposes revenue metrics (estimatedRevenue, cpm, ...) but the OAuth flow never requested the monetary scope, so every monetary query returned 401 "Insufficient permission". Add yt-analytics-monetary.readonly to SCOPES so revenue reads work after a re-login, plus a regression test pinning the scope. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/ytstudio/api.py | 4 ++++ tests/test_api.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/ytstudio/api.py b/src/ytstudio/api.py index 48f11f6..178be14 100644 --- a/src/ytstudio/api.py +++ b/src/ytstudio/api.py @@ -63,6 +63,10 @@ def api(request): "https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/youtube.force-ssl", "https://www.googleapis.com/auth/yt-analytics.readonly", + # Required to read revenue/earnings metrics (estimatedRevenue, cpm, ...). + # Without it the Analytics API returns 401 "Insufficient permission" for + # the monetary metrics that the registry already exposes. + "https://www.googleapis.com/auth/yt-analytics-monetary.readonly", ] HEADLESS_REDIRECT_URI = "http://127.0.0.1:9876/" diff --git a/tests/test_api.py b/tests/test_api.py index 076a444..a063243 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -20,6 +20,13 @@ def make_http_error(status: int, reason: str = ""): return error +class TestScopes: + def test_includes_monetary_analytics_scope(self): + # Revenue/earnings metrics (estimatedRevenue, cpm, ...) require this + # scope; without it the Analytics API returns 401 for monetary reports. + assert "https://www.googleapis.com/auth/yt-analytics-monetary.readonly" in api_module.SCOPES + + class TestHandleApiError: def test_quota_exceeded_exits(self): error = make_http_error(403, "quotaExceeded")