Spaces:
Sleeping
Sleeping
File size: 2,639 Bytes
97ec0e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import log from '../src/utils/logger.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ACCOUNTS_FILE = path.join(__dirname, '..', 'data', 'accounts.json');
const CLIENT_ID = '1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com';
const CLIENT_SECRET = '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';
async function refreshToken(refreshToken) {
const body = new URLSearchParams({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: refreshToken
});
const response = await fetch('https://oauth2.googleapis.com/token', {
method: 'POST',
headers: {
'Host': 'oauth2.googleapis.com',
'User-Agent': 'Go-http-client/1.1',
'Content-Length': body.toString().length.toString(),
'Content-Type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'gzip'
},
body: body.toString()
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return await response.json();
}
async function refreshAllTokens() {
if (!fs.existsSync(ACCOUNTS_FILE)) {
log.error(`文件不存在: ${ACCOUNTS_FILE}`);
process.exit(1);
}
const accounts = JSON.parse(fs.readFileSync(ACCOUNTS_FILE, 'utf-8'));
log.info(`找到 ${accounts.length} 个账号`);
let successCount = 0;
let failCount = 0;
for (let i = 0; i < accounts.length; i++) {
const account = accounts[i];
if (account.enable === false) {
log.warn(`账号 ${i + 1}: 已禁用,跳过`);
continue;
}
try {
log.info(`刷新账号 ${i + 1}...`);
const tokenData = await refreshToken(account.refresh_token);
account.access_token = tokenData.access_token;
account.expires_in = tokenData.expires_in;
account.timestamp = Date.now();
successCount++;
log.info(`账号 ${i + 1}: 刷新成功`);
} catch (error) {
failCount++;
log.error(`账号 ${i + 1}: 刷新失败 - ${error.message}`);
if (error.message.includes('invalid_grant') || error.message.includes('400')) {
account.enable = false;
log.warn(`账号 ${i + 1}: Token 已失效或错误,已自动禁用该账号`);
}
}
}
fs.writeFileSync(ACCOUNTS_FILE, JSON.stringify(accounts, null, 2));
log.info(`刷新完成: 成功 ${successCount} 个, 失败 ${failCount} 个`);
}
refreshAllTokens().catch(err => {
log.error('刷新失败:', err.message);
process.exit(1);
});
|