@luoye #13
(async () => {
try {
const sessionResp = await fetch('/api/auth/session', {
credentials: 'include'
});if (!sessionResp.ok)
throw new Error('获取 session 失败,请先登录 chatgpt.com');
const session = await sessionResp.json();
const accessToken = session.accessToken;
if (!accessToken)
throw new Error('没有 accessToken,请先登录 chatgpt.com');
const headers = {
Authorization: `Bearer ${accessToken}`,
Accept: 'application/json',
'Content-Type': 'application/json',
'oai-language': 'zh-Hant',
'oai-device-id': crypto.randomUUID(),
};
const getJson = async (url, options = {}) => {
const r = await fetch(url, { headers, ...options });
const text = await r.text();
if (!r.ok) {
// 特殊处理权限错误
if (r.status === 401 && text.includes('Only account owners can perform this action')) {
console.warn(`⚠️ 权限不足:你不是该账户的 Owner,无法访问 ${url}`);
return null; // 不抛出错误,让流程继续
}
throw new Error(`${url} HTTP ${r.status}: ${text.slice(0, 300)}`);
}
return JSON.parse(text);
};
// 获取账户列表
const accountCheck = await getJson('/backend-api/accounts/check/v4-2023-04-27');
const accounts = accountCheck.accounts || {};
// 优先使用 session 中的主账户 ID(如果你是 Owner,这通常就是你自己的账户)
const primaryAccountId = session.account?.id;
// 如果没有主账户 ID,再尝试取第一个
let accountId = primaryAccountId;
if (!accountId) {
const firstKey = Object.keys(accounts)[0];
accountId = accounts[firstKey]?.account?.account_id || firstKey;
}
if (!accountId) throw new Error('未找到有效 account_id');
console.log(`使用的 Account ID: ${accountId}`);
console.log(`当前账户类型: ${session.account?.planType || '未知'}`);
console.log('正在拉取账单信息(如果你不是 Owner,支付接口会跳过)...\n');
// 并行请求,但支付类接口如果失败不会阻塞
const [invoices, paymentMethods, billingInfo] = await Promise.all([
getJson(`/backend-api/invoices?limit=10&account_id=${encodeURIComponent(accountId)}`),
getJson(`/backend-api/payments/payment_methods?account_id=${encodeURIComponent(accountId)}`),
getJson(`/backend-api/payments/billing_info?account_id=${encodeURIComponent(accountId)}`),
]);
const result = {
email: session.user?.email,
plan: session.account?.planType || 'unknown',
accountId,
accessTokenStatus: 'exists (已自动获取)',
invoices,
paymentMethods,
billingInfo,
};
console.log('✅ 账单提取结果:', result);
if (invoices === null || paymentMethods === null || billingInfo === null) {
console.warn('⚠️ 部分支付信息无法获取,因为你不是该账户的 Owner。');
console.warn('👉 请让工作区 Owner 使用此脚本,或切换到个人订阅的账号。');
}
console.log('账单管理页:', `https://chatgpt.com/account/manage?account_id=${encodeURIComponent(accountId)}`);
// 挂到全局方便查看
window.__billingResult = result;
return result;
} catch (err) {console.error('❌ 脚本执行出错:', err.message);}})();
控制台执行,可以看下月账单