-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgetToken.js
69 lines (62 loc) · 2.1 KB
/
getToken.js
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
const fs = require('fs/promises');
const axios = require('axios');
const reffCode = "OwAG3kib1ivOJG4Y0OCZ8lJETa6ypvsDtGmdhcjA";
async function readAccounts(filePath) {
try {
const data = await fs.readFile(filePath, 'utf8');
const accounts = data.split('\n').map(line => {
const [email, password] = line.split('|');
return { email, password };
});
return accounts;
} catch (err) {
throw new Error(`Error reading accounts file: ${err.message}`);
}
}
async function login(email, password) {
const loginUrl = "https://auth.teneo.pro/api/login";
try {
const response = await axios.post(loginUrl, {
email: email,
password: password
}, {
headers: {
'x-api-key': reffCode
}
});
const access_token = response.data.access_token;
return access_token;
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
return null;
}
}
async function refreshTokens() {
console.log('Processing getting token to all accounts...');
try {
await fs.access('tokens.txt');
await fs.unlink('tokens.txt');
console.log('clear old tokens.txt successfully');
} catch (error) {
console.log('file tokens.txt does not exist, creating new file');
}
try {
const accounts = await readAccounts('accounts.txt');
if (!accounts.length) {
console.log('No accounts found in the accounts.txt');
return;
}
for (const account of accounts) {
console.log('Getting access token for:', account.email);
const token = await login(account.email, account.password);
if (token) {
console.log('Access token:', token);
await fs.appendFile('tokens.txt', token + '\n', 'utf8');
console.log('Token saved to file tokens.txt');
}
}
} catch (error) {
console.error('Error processing accounts:', error.message);
}
}
refreshTokens();