"use client"; import { useState } from "react"; import { Check, Copy, ExternalLink } from "lucide-react"; import { fetchJson } from "@/lib/api"; const DEFAULT_PROJECT = "live-473104"; const DEFAULT_REDIRECT = "https://dylive.nomadro.com/api/shorts/oauth/callback"; function gcpLinks(projectId: string) { const p = encodeURIComponent(projectId || DEFAULT_PROJECT); return { enableApi: `https://console.cloud.google.com/apis/library/youtube.googleapis.com?project=${p}`, consent: `https://console.cloud.google.com/apis/credentials/consent?project=${p}`, createClient: `https://console.cloud.google.com/apis/credentials/oauthclient?project=${p}`, credentials: `https://console.cloud.google.com/apis/credentials?project=${p}`, }; } type Props = { initialProjectId?: string; initialClientId?: string; initialClientSecret?: string; redirectUri?: string; configured?: boolean; onSaved?: () => void; }; export function GoogleOauthSetupWizard({ initialProjectId = DEFAULT_PROJECT, initialClientId = "", initialClientSecret = "", redirectUri = DEFAULT_REDIRECT, configured = false, onSaved, }: Props) { const [projectId, setProjectId] = useState(initialProjectId || DEFAULT_PROJECT); const [clientId, setClientId] = useState(initialClientId); const [clientSecret, setClientSecret] = useState(initialClientSecret); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(""); const [error, setError] = useState(""); const [copied, setCopied] = useState(false); const links = gcpLinks(projectId); async function copyRedirect() { try { await navigator.clipboard.writeText(redirectUri); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch { setError("复制失败,请手动选中下方 URI"); } } function openAll() { // User gesture required; open critical pages in order. window.open(links.enableApi, "_blank", "noopener,noreferrer"); window.setTimeout(() => window.open(links.consent, "_blank", "noopener,noreferrer"), 400); window.setTimeout(() => window.open(links.createClient, "_blank", "noopener,noreferrer"), 800); } async function saveClient() { setBusy(true); setMsg(""); setError(""); try { if (!clientId.trim() || !clientSecret.trim()) { throw new Error("请粘贴 Client ID 和 Client Secret"); } await fetchJson("/api/settings", { method: "PATCH", body: JSON.stringify({ shorts_oauth_client_id: clientId.trim(), shorts_oauth_client_secret: clientSecret.trim(), shorts_oauth_redirect_uri: redirectUri, shorts_gcp_project_id: projectId.trim() || DEFAULT_PROJECT, }), }); setMsg("已保存。下面给频道点「Google 授权」即可。"); onSaved?.(); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } return (

Google 一键引导(无法代建客户端)

Google 禁止第三方替你创建 OAuth 客户端。点下面按钮直达你的项目页面,创建后粘贴回来保存——频道授权才是真正一键。

{configured ? ( Client 已配置 ) : ( 待配置 Client )}
已有凭据列表
  1. 弹出页里启用 YouTube Data API v3(建议同时启用 YouTube Analytics API)
  2. 同意屏幕选 External,加你自己为 Test user;Scopes 尽量加全: youtube、 youtube.force-ssl、 youtube.upload、 youtube.readonly、 youtubepartner、 yt-analytics.readonly
  3. 创建 Web application,重定向 URI 填下面这行(先复制)
  4. 把 Client ID / Secret 粘贴到下方 → 保存
  5. 权限变更后,到 Shorts 页对每个频道点「重新授权」才会拿到新 scope
{redirectUri}
{msg ?

{msg}

: null} {error ?

{error}

: null}
); }