From d99153175ab57741f171453d5a6400a0dfaf6d81 Mon Sep 17 00:00:00 2001 From: eric Date: Sat, 22 Aug 2026 01:29:05 -0500 Subject: [PATCH] fix: clear oauth_ok after re-auth, show Google brand hints, reduce false OAuth errors Co-authored-by: Cursor --- backend/app/shorts_oauth.py | 8 ++++- backend/app/shorts_pipeline.py | 8 +++-- backend/app/shorts_schedule.py | 1 + frontend/src/app/shorts/page.tsx | 16 +++++++--- .../src/components/YtChannelMetaBadges.tsx | 17 ++++++++-- frontend/src/lib/ytBrandHints.ts | 31 +++++++++++++++++++ 6 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 frontend/src/lib/ytBrandHints.ts diff --git a/backend/app/shorts_oauth.py b/backend/app/shorts_oauth.py index 85a2bf3..2a3051e 100644 --- a/backend/app/shorts_oauth.py +++ b/backend/app/shorts_oauth.py @@ -413,7 +413,13 @@ async def exchange_code(*, code: str, state: str, request_base: str = "") -> dic name = str(channel.get("name") or "").strip() if yt_title and (not name or name.startswith("Shorts") or name == "未命名频道"): patch["name"] = yt_title - updated = db.update_shorts_channel(channel_pk, **patch) + updated = db.update_shorts_channel( + channel_pk, + **patch, + oauth_ok=1, + oauth_checked_at=time.time(), + oauth_error="", + ) try: from . import yt_long_db diff --git a/backend/app/shorts_pipeline.py b/backend/app/shorts_pipeline.py index 15c77b3..58c4e41 100644 --- a/backend/app/shorts_pipeline.py +++ b/backend/app/shorts_pipeline.py @@ -503,13 +503,17 @@ class ShortsPipeline: try: for ch in db.list_shorts_channels(enabled=True): ok_flag = int(ch.get("oauth_ok") if ch.get("oauth_ok") is not None else -1) - if ok_flag == 0 or not str(ch.get("refresh_token") or "").strip(): + err = str(ch.get("oauth_error") or "") + has_rt = bool(str(ch.get("refresh_token") or "").strip()) + if ok_flag == 0 or not has_rt: + if has_rt and "重授权中" in err: + continue oauth_issues.append( { "id": ch.get("id"), "name": ch.get("name"), "oauth_ok": ok_flag, - "oauth_error": ch.get("oauth_error") or ("未授权" if not str(ch.get("refresh_token") or "").strip() else ""), + "oauth_error": err or ("未授权" if not has_rt else ""), "oauth_checked_at": ch.get("oauth_checked_at") or 0, } ) diff --git a/backend/app/shorts_schedule.py b/backend/app/shorts_schedule.py index 94fb191..ea501cd 100644 --- a/backend/app/shorts_schedule.py +++ b/backend/app/shorts_schedule.py @@ -256,6 +256,7 @@ def schedule_board(limit: int = 24) -> dict[str, Any]: "last_live_url": str(c.get("last_live_url") or ""), "oauth_ok": c.get("oauth_ok"), "oauth_error": str(c.get("oauth_error") or ""), + "note": str(c.get("note") or ""), "ypp_ok": ypp_by_shorts.get(cid, -1), **stats, } diff --git a/frontend/src/app/shorts/page.tsx b/frontend/src/app/shorts/page.tsx index 04cc8a4..e74a748 100644 --- a/frontend/src/app/shorts/page.tsx +++ b/frontend/src/app/shorts/page.tsx @@ -31,7 +31,7 @@ import { import { UploadTimeline } from "@/components/UploadTimeline"; import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge"; import { YtChannelProfileSnippet, type YtProfileSnippet } from "@/components/YtChannelProfileSnippet"; -import type { YtChannelMeta } from "@/components/YtChannelMetaBadges"; +import { brandHintForChannel } from "@/lib/ytBrandHints"; import { SessionCharts, type RelayStats } from "@/components/SessionCharts"; import { PageLoader, TableSkeleton } from "@/components/ui"; import { fetchJson, fmtDuration, fmtTs, todayBeijingISO, shiftBeijingISO } from "@/lib/api"; @@ -88,6 +88,7 @@ type BoardChannel = { ypp_ok?: number; oauth_ok?: number; oauth_error?: string; + note?: string; last_status?: string; last_title?: string; last_live_url?: string; @@ -402,12 +403,17 @@ export default function ShortsPage() { }>; function channelMeta(ch: BoardChannel): YtChannelMeta { - const oauthBad = oauthIssues.some((x) => x.id === ch.id); + const issue = oauthIssues.find((x) => x.id === ch.id); + const oauthBad = Boolean(issue); + const staleReauth = Boolean( + ch.authorized && issue?.oauth_error?.includes("重授权中"), + ); return { enabled: ch.enabled, authorized: ch.authorized, - oauthOk: oauthBad ? 0 : ch.oauth_ok, - oauthError: oauthBad ? oauthIssues.find((x) => x.id === ch.id)?.oauth_error : ch.oauth_error, + oauthOk: staleReauth ? 1 : oauthBad ? 0 : ch.oauth_ok, + oauthError: issue?.oauth_error || ch.oauth_error, + brandHint: brandHintForChannel(ch.yt_profile, ch.youtube_channel_id, ch.note), yppOk: ch.ypp_ok, oauthProfileId: ch.oauth_profile_id, }; @@ -459,7 +465,7 @@ export default function ShortsPage() { if (hasBound && needTitle) { const ok = window.confirm( `本卡需要的 YouTube 频道:\n${needTitle}${needHandle ? ` ${needHandle}` : ""}\n\n` + - `系统将自动撤销旧 OAuth token,再打开 Google 重新授权(含 YPP 获利所需 force-ssl 权限)。\n` + + `Google 授权页请选品牌账号:${brandHintForChannel(board?.yt_profile, full?.youtube_channel_id || board?.youtube_channel_id, full?.note || board?.note) || "(见卡片 Google·… 提示)"}\n\n` + `Google 品牌账号名可能和上面不一样;点错了页面会提示,不会写入。\n\n继续?` ); if (!ok) return; diff --git a/frontend/src/components/YtChannelMetaBadges.tsx b/frontend/src/components/YtChannelMetaBadges.tsx index 6d35e73..5f0bc5b 100644 --- a/frontend/src/components/YtChannelMetaBadges.tsx +++ b/frontend/src/components/YtChannelMetaBadges.tsx @@ -7,6 +7,8 @@ export type YtChannelMeta = { authorized?: boolean; oauthOk?: number | boolean | null; oauthError?: string; + /** Google brand-account name shown in OAuth picker (often ≠ YouTube title). */ + brandHint?: string; /** -1 unknown, 0 not YPP, 1 YPP */ yppOk?: number | null; oauthProfileId?: string; @@ -18,8 +20,12 @@ function oauthTone(oauthOk?: number | boolean | null, authorized?: boolean) { return "text-amber-300"; } -function oauthLabel(oauthOk?: number | boolean | null, authorized?: boolean) { - if (oauthOk === 0 || oauthOk === false) return "OAuth 异常"; +function oauthLabel(oauthOk?: number | boolean | null, authorized?: boolean, oauthError?: string) { + if (oauthOk === 0 || oauthOk === false) { + if (oauthError?.includes("重授权中")) return "授权写入中"; + if (oauthError?.includes("force-ssl")) return "缺 YPP 权限"; + return "OAuth 异常"; + } if (authorized) return "已授权"; return "未授权"; } @@ -49,8 +55,13 @@ export function YtChannelMetaBadges({ className={`${badge} ${oauthTone(meta.oauthOk, meta.authorized)}`} title={meta.oauthError || undefined} > - {oauthLabel(meta.oauthOk, meta.authorized)} + {oauthLabel(meta.oauthOk, meta.authorized, meta.oauthError)} + {meta.brandHint ? ( + + Google·{meta.brandHint} + + ) : null} {meta.yppOk === 1 ? ( YPP·获利 diff --git a/frontend/src/lib/ytBrandHints.ts b/frontend/src/lib/ytBrandHints.ts new file mode 100644 index 0000000..030e1a8 --- /dev/null +++ b/frontend/src/lib/ytBrandHints.ts @@ -0,0 +1,31 @@ +/** Google OAuth picker brand name ↔ YouTube channel (title/handle often differ). */ +export const YT_OAUTH_BRAND_BY_HANDLE: Record = { + "@ewfwefwefewf": "digitl nomad", + "@nomadstreetfood": "hackrobot-jp", + "@nomadrohub": "nomad Immigration", + "@evanstreetfood": "Nhat Vy", + "@nomadvloglife": "Lyca Hack", + "@walkingtour-c8t": "walking tour", +}; + +export function normalizeYtHandle(raw?: string | null): string { + const s = String(raw || "").trim(); + if (!s) return ""; + if (s.startsWith("@")) return s; + if (s.startsWith("UC")) return s; + return `@${s.replace(/^@/, "")}`; +} + +export function brandHintForChannel(profile?: { custom_url?: string; title?: string } | null, fallbackChannelId?: string, note?: string): string { + const fromNote = String(note || "").match(/Google品牌[::]\s*(.+)/i)?.[1]?.trim(); + if (fromNote) return fromNote; + const handle = normalizeYtHandle(profile?.custom_url || fallbackChannelId); + if (handle && YT_OAUTH_BRAND_BY_HANDLE[handle]) return YT_OAUTH_BRAND_BY_HANDLE[handle]; + const title = String(profile?.title || "").trim().toLowerCase(); + if (title.includes("nomad vlog")) return "Lyca Hack"; + if (title.includes("nomad street food")) return "hackrobot-jp"; + if (title.includes("nomadro hub")) return "nomad Immigration"; + if (title.includes("walking tour")) return "walking tour"; + if (title.includes("nomdfsfbjksfbsjk")) return "digitl nomad"; + return ""; +}