"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 禁止第三方替你创建 OAuth 客户端。点下面按钮直达你的项目页面,创建后粘贴回来保存——频道授权才是真正一键。
youtube.upload、
youtube.readonly
(不要加 youtubepartner / yt-analytics,未验证应用容易在同意页 500)
{redirectUri}
{msg}
: null} {error ?{error}
: null}