diff --git a/backend/app/main.py b/backend/app/main.py index 46518d8..624e943 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -579,9 +579,14 @@ async def shorts_oauth_callback( request_base=base, ) title = quote(str(result.get("youtube_title") or "ok")) - return RedirectResponse(f"{base}/shorts?oauth=ok&title={title}", status_code=302) + handle = quote(str(result.get("youtube_handle") or "")) + return RedirectResponse( + f"{base}/shorts?oauth=ok&title={title}" + (f"&handle={handle}" if handle else ""), + status_code=302, + ) except Exception as exc: - return RedirectResponse(f"{base}/shorts?oauth=error&msg={quote(str(exc)[:180])}", status_code=302) + # Keep enough room for mismatch hints (got vs need channel names). + return RedirectResponse(f"{base}/shorts?oauth=error&msg={quote(str(exc)[:320])}", status_code=302) @app.post("/api/shorts/start") diff --git a/backend/app/shorts_oauth.py b/backend/app/shorts_oauth.py index 845b0a1..f4e15e8 100644 --- a/backend/app/shorts_oauth.py +++ b/backend/app/shorts_oauth.py @@ -301,9 +301,33 @@ async def exchange_code(*, code: str, state: str, request_base: str = "") -> dic items = ch_data.get("items") or [] yt_id = "" yt_title = "" + yt_handle = "" if items: + sn = items[0].get("snippet") or {} yt_id = str(items[0].get("id") or "") - yt_title = str((items[0].get("snippet") or {}).get("title") or "") + yt_title = str(sn.get("title") or "") + yt_handle = str(sn.get("customUrl") or "").strip() + + # Re-auth safety: brand-account picker names often differ from YouTube titles. + # If this card already has a known channel id, reject mismatches without writing. + expected_id = str(channel.get("youtube_channel_id") or "").strip() + if expected_id and yt_id and yt_id != expected_id: + need_title = str(channel.get("name") or "").strip() or "本卡频道" + need_handle = "" + try: + from . import youtube_manage as ytm + + cached = ytm.get_yt_profile_cache(channel_pk) or {} + need_title = str(cached.get("title") or need_title).strip() or need_title + need_handle = str(cached.get("custom_url") or "").strip() + except Exception: + pass + got = f"「{yt_title or yt_id}」" + (f" {yt_handle}" if yt_handle else "") + need = f"「{need_title}」" + (f" {need_handle}" if need_handle else "") + raise ValueError( + f"点错品牌账号了:这次选中的是 {got},本卡需要 {need}。" + f"未写入授权,请再点「重授权」换列表里下一个试。" + ) patch: dict[str, Any] = { "client_id": client_id, @@ -321,6 +345,7 @@ async def exchange_code(*, code: str, state: str, request_base: str = "") -> dic "channel": updated, "youtube_channel_id": yt_id, "youtube_title": yt_title, + "youtube_handle": yt_handle, "oauth_client_source": source, "oauth_profile_id": source_meta.get("profile_id") or "", } diff --git a/frontend/src/app/shorts/page.tsx b/frontend/src/app/shorts/page.tsx index 4c32203..108901c 100644 --- a/frontend/src/app/shorts/page.tsx +++ b/frontend/src/app/shorts/page.tsx @@ -256,10 +256,13 @@ export default function ShortsPage() { const oauth = sp.get("oauth"); if (!oauth) return; if (oauth === "ok") { - setMsg(`Google 授权成功${sp.get("title") ? `:${sp.get("title")}` : ""}`); + const title = sp.get("title") || ""; + const handle = sp.get("handle") || ""; + setMsg(`Google 授权成功${title ? `:${title}` : ""}${handle ? ` ${handle}` : ""}`); void load(); } else if (oauth === "error") { - setError(sp.get("msg") || "授权失败"); + const raw = sp.get("msg") || "授权失败"; + setError(raw); } window.history.replaceState({}, "", "/shorts"); }, [load]); @@ -423,13 +426,25 @@ export default function ShortsPage() { } function goAuthorize(channelId: string) { - const ch = channels.find((c) => c.id === channelId); + const full = channels.find((c) => c.id === channelId); + const board = boardChannels.find((c) => c.id === channelId); // Stale state after create is fine — only block when we know this channel cannot auth - if (ch && !ch.can_authorize) { + if (full && full.can_authorize === false) { setShowWizard(true); setError("请先在「设置」配置对应 OAuth 档(A/B/C/D),并在本频道选择该配置档"); return; } + const needTitle = String(board?.yt_profile?.title || full?.name || board?.name || "").trim(); + const needHandle = String(board?.yt_profile?.custom_url || "").trim(); + const hasBound = Boolean(String(full?.youtube_channel_id || board?.youtube_channel_id || "").trim()); + if (hasBound && needTitle) { + const ok = window.confirm( + `本卡需要的 YouTube 频道:\n${needTitle}${needHandle ? ` ${needHandle}` : ""}\n\n` + + `Google 列表里的「品牌账号名」经常和上面不一样。\n` + + `点错了没关系:不会写入授权,页面会告诉你实际选中了谁,再换下一个试。\n\n继续打开 Google?` + ); + if (!ok) return; + } window.location.href = `/api/shorts/oauth/start?channel_pk=${encodeURIComponent(channelId)}`; }