douyinWEB/scripts/_fix_oauth_refresh_token.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

110 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy OAuth refresh_token fix + recreate channel A + set DY_PUBLIC_BASE_URL."""
from __future__ import annotations
import json
import sys
import time
from pathlib import Path
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
FILES = {
"shorts_oauth.py": ROOT / "backend" / "app" / "shorts_oauth.py",
}
VPS = "107.173.30.245"
GUEST_PORT, GUEST_USER, GUEST_PASS = 4014, "eric", "4669805"
SECRET = "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"
CID = "685779248822-dnrq7043r03rlviktjhr8orre8e273gc.apps.googleusercontent.com"
def main():
g = paramiko.SSHClient()
g.set_missing_host_key_policy(paramiko.AutoAddPolicy())
g.connect(VPS, GUEST_PORT, GUEST_USER, GUEST_PASS, timeout=40, banner_timeout=80, allow_agent=False, look_for_keys=False)
sftp = g.open_sftp()
sftp.put(str(FILES["shorts_oauth.py"]), "/tmp/_up_shorts_oauth.py")
sftp.close()
remote = rf"""
echo '{GUEST_PASS}' | sudo -S -p '' bash -lc '
set -e
install -m 644 /tmp/_up_shorts_oauth.py /opt/douyinWEB/backend/app/shorts_oauth.py
# ensure public base for OAuth redirects
API=/etc/systemd/system/douyinweb-api.service
grep -q DY_PUBLIC_BASE_URL=https://dy.nomadro.com "$API" || \
sed -i "/\[Service\]/a Environment=DY_PUBLIC_BASE_URL=https://dy.nomadro.com" "$API"
systemctl daemon-reload
systemctl restart douyinweb-api
sleep 2
cd /opt/douyinWEB/backend
.venv/bin/python - <<PY
from app import db, shorts_oauth
import json, time
# sync client into profile A + global
cfg = db.get_settings_map()
profs = shorts_oauth.normalize_oauth_profiles(cfg.get("shorts_oauth_profiles"))
for i, p in enumerate(profs):
if p["id"] == "A":
profs[i] = {{
**p,
"client_id": "{CID}",
"client_secret": "{SECRET}",
"label": p.get("label") or "谷歌账号 A",
}}
db.update_settings_map({{
"shorts_oauth_profiles": profs,
"shorts_oauth_client_id": "{CID}",
"shorts_oauth_client_secret": "{SECRET}",
"shorts_oauth_redirect_uri": "https://dy.nomadro.com/api/shorts/oauth/callback",
}})
# recreate one channel if empty
rows = db.list_shorts_channels()
print("before", len(rows))
if not rows:
ch = db.create_shorts_channel(name="未命名频道", oauth_profile_id="A", enabled=True, daily_min=9, daily_max=9)
print("created", ch["id"])
else:
for r in rows:
print("keep", r.get("id"), r.get("name"), r.get("oauth_error"))
ch = rows[0]
print("redirect", shorts_oauth.resolve_redirect_uri("https://dy.nomadro.com"))
print("has_include_granted", "include_granted_scopes" in open("/opt/douyinWEB/backend/app/shorts_oauth.py", encoding="utf-8").read())
PY
for i in $(seq 1 15); do
code=$(curl -sS -m 3 -o /dev/null -w "%{{http_code}}" http://127.0.0.1:8014/api/health || echo x)
echo health $code
[ "$code" = "200" ] && break
sleep 2
done
'
"""
_, o, e = g.exec_command(remote, timeout=120)
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
g.close()
with httpx.Client(base_url="https://dy.nomadro.com", timeout=40) as c:
c.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
st = c.get("/api/shorts/status").json()
print("redirect", st.get("oauth_redirect_uri"))
board = st.get("board") or {}
chs = board.get("channels") or []
print("n", len(chs), "issues", st.get("oauth_issues"))
for ch in chs:
print(json.dumps({k: ch.get(k) for k in ("id", "name", "oauth_profile_id", "oauth_error", "authorized")}, ensure_ascii=False))
if chs:
pk = chs[0]["id"]
r = c.get(f"/api/shorts/oauth/start?channel_pk={pk}", follow_redirects=False)
loc = r.headers.get("location") or ""
print("start", r.status_code, "has_include", "include_granted" in loc, "prompt", "prompt=consent" in loc or "consent" in loc)
if __name__ == "__main__":
main()