199 lines
7.6 KiB
TypeScript
199 lines
7.6 KiB
TypeScript
"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 (
|
||
<div className="mb-4 space-y-3 rounded-xl border border-amber-500/30 bg-amber-500/5 p-4 text-sm">
|
||
<div className="flex flex-wrap items-start justify-between gap-2">
|
||
<div>
|
||
<h3 className="font-medium text-amber-100">Google 一键引导(无法代建客户端)</h3>
|
||
<p className="mt-1 text-xs text-slate-400">
|
||
Google 禁止第三方替你创建 OAuth 客户端。点下面按钮直达你的项目页面,创建后粘贴回来保存——频道授权才是真正一键。
|
||
</p>
|
||
</div>
|
||
{configured ? (
|
||
<span className="rounded-lg border border-teal-500/30 bg-teal-500/10 px-2 py-1 text-xs text-teal-200">Client 已配置</span>
|
||
) : (
|
||
<span className="rounded-lg border border-amber-500/30 bg-amber-500/10 px-2 py-1 text-xs text-amber-100">待配置 Client</span>
|
||
)}
|
||
</div>
|
||
|
||
<label className="block text-xs text-slate-400">
|
||
GCP Project ID
|
||
<input
|
||
value={projectId}
|
||
onChange={(e) => setProjectId(e.target.value)}
|
||
className="mt-1 w-full max-w-md rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm font-mono"
|
||
/>
|
||
</label>
|
||
|
||
<div className="flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={openAll}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-[#ff2d55] px-3 py-2 text-sm text-white"
|
||
>
|
||
<ExternalLink className="h-3.5 w-3.5" />
|
||
一键打开:启用 API + 同意屏幕 + 创建客户端
|
||
</button>
|
||
<a
|
||
href={links.credentials}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="inline-flex items-center gap-1 rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm"
|
||
>
|
||
已有凭据列表
|
||
</a>
|
||
</div>
|
||
|
||
<ol className="list-decimal space-y-1 pl-5 text-xs text-slate-400">
|
||
<li>弹出页里启用 YouTube Data API v3(建议同时启用 YouTube Analytics API)</li>
|
||
<li>
|
||
同意屏幕选 External,加你自己为 Test user;Scopes 尽量加全:
|
||
<code className="ml-1 text-[10px] text-slate-300">youtube</code>、
|
||
<code className="text-[10px] text-slate-300">youtube.force-ssl</code>、
|
||
<code className="text-[10px] text-slate-300">youtube.upload</code>、
|
||
<code className="text-[10px] text-slate-300">youtube.readonly</code>、
|
||
<code className="text-[10px] text-slate-300">youtubepartner</code>、
|
||
<code className="text-[10px] text-slate-300">yt-analytics.readonly</code>
|
||
</li>
|
||
<li>
|
||
创建 <b className="text-slate-200">Web application</b>,重定向 URI 填下面这行(先复制)
|
||
</li>
|
||
<li>把 Client ID / Secret 粘贴到下方 → 保存</li>
|
||
<li>权限变更后,到 Shorts 页对每个频道点「重新授权」才会拿到新 scope</li>
|
||
</ol>
|
||
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<code className="rounded bg-black/50 px-2 py-1 font-mono text-[11px] text-teal-200">{redirectUri}</code>
|
||
<button
|
||
type="button"
|
||
onClick={() => void copyRedirect()}
|
||
className="inline-flex items-center gap-1 rounded border border-white/10 bg-white/5 px-2 py-1 text-xs"
|
||
>
|
||
{copied ? <Check className="h-3 w-3 text-teal-300" /> : <Copy className="h-3 w-3" />}
|
||
{copied ? "已复制" : "复制回调 URI"}
|
||
</button>
|
||
</div>
|
||
|
||
<div className="grid gap-2 md:grid-cols-2">
|
||
<label className="block text-xs text-slate-400">
|
||
Client ID
|
||
<input
|
||
value={clientId}
|
||
onChange={(e) => setClientId(e.target.value)}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
placeholder="xxxx.apps.googleusercontent.com"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-slate-400">
|
||
Client Secret
|
||
<input
|
||
type="password"
|
||
value={clientSecret}
|
||
onChange={(e) => setClientSecret(e.target.value)}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
placeholder="GOCSPX-..."
|
||
/>
|
||
</label>
|
||
</div>
|
||
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void saveClient()}
|
||
className="rounded-lg border border-teal-500/30 bg-teal-500/15 px-3 py-2 text-sm text-teal-100"
|
||
>
|
||
{busy ? "保存中…" : "保存 Client 到本站"}
|
||
</button>
|
||
{msg ? <p className="text-xs text-teal-300">{msg}</p> : null}
|
||
{error ? <p className="text-xs text-rose-300">{error}</p> : null}
|
||
</div>
|
||
);
|
||
}
|