-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathoauth-server.js
More file actions
150 lines (123 loc) · 4.57 KB
/
Copy pathoauth-server.js
File metadata and controls
150 lines (123 loc) · 4.57 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const express = require('express');
const { google } = require('googleapis');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
class OAuthServer {
constructor() {
this.app = express();
this.server = null;
this.setupRoutes();
}
setupRoutes() {
this.app.get('/auth/callback', async (req, res) => {
const { code, error } = req.query;
if (error) {
res.send(`<h1>❌ Authentication Error</h1><p>${error}</p>`);
return;
}
if (!code) {
res.send(`<h1>❌ No Authorization Code</h1><p>No authorization code received</p>`);
return;
}
try {
// Exchange code for tokens
await this.exchangeCodeForTokens(code);
res.send(`
<h1>🎉 Authentication Successful!</h1>
<p>Your YouTube Automation Agent has been successfully authorized!</p>
<p>You can close this window and return to the terminal.</p>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
h1 { color: #4CAF50; }
</style>
`);
console.log(chalk.green('\n✅ Authentication successful! Starting YouTube Automation Agent...'));
// Close the server after successful auth
setTimeout(() => {
this.server.close();
this.startMainApplication();
}, 2000);
} catch (error) {
console.error(chalk.red('Token exchange failed:'), error);
res.send(`<h1>❌ Token Exchange Failed</h1><p>${error.message}</p>`);
}
});
this.app.get('/', (req, res) => {
res.send(`
<h1>🎬 YouTube Automation Agent</h1>
<p>OAuth callback server is running...</p>
<p>Please complete the authorization process.</p>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
</style>
`);
});
}
async exchangeCodeForTokens(code) {
const credentialsPath = path.join(__dirname, 'config', 'credentials.json');
const tokensPath = path.join(__dirname, 'config', 'tokens.json');
const credentials = JSON.parse(fs.readFileSync(credentialsPath));
const oauth2Client = new google.auth.OAuth2(
credentials.youtube.client_id,
credentials.youtube.client_secret,
credentials.youtube.redirect_uris[0]
);
const { tokens } = await oauth2Client.getToken(code);
// Save tokens
const tokenData = {
youtube: tokens
};
fs.writeFileSync(tokensPath, JSON.stringify(tokenData, null, 2));
console.log(chalk.green('✅ Tokens saved successfully!'));
}
async startMainApplication() {
console.log(chalk.cyan('\n🚀 Starting YouTube Automation Agent...'));
// Import and start the main application
const { spawn } = require('child_process');
const mainApp = spawn('node', ['index.js'], {
stdio: 'inherit',
cwd: __dirname
});
mainApp.on('close', (code) => {
console.log(chalk.yellow(`Main application exited with code ${code}`));
});
}
start() {
this.server = this.app.listen(8080, () => {
console.log(chalk.cyan('\n🔐 OAuth callback server started on http://localhost:8080'));
this.generateAuthUrl();
});
}
generateAuthUrl() {
const credentialsPath = path.join(__dirname, 'config', 'credentials.json');
const credentials = JSON.parse(fs.readFileSync(credentialsPath));
const oauth2Client = new google.auth.OAuth2(
credentials.youtube.client_id,
credentials.youtube.client_secret,
credentials.youtube.redirect_uris[0]
);
const scopes = [
'https://www.googleapis.com/auth/youtube.upload',
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/yt-analytics.readonly'
];
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
prompt: 'consent'
});
console.log(chalk.cyan.bold('\n🎬 YouTube Automation Agent - OAuth Setup'));
console.log(chalk.gray('═'.repeat(60)));
console.log(chalk.cyan('\n🔗 Please visit this URL to authorize the application:'));
console.log(chalk.blue.underline(authUrl));
console.log(chalk.yellow('\nAfter authorization, you will be redirected back automatically.'));
console.log(chalk.gray('The OAuth server will handle the rest!'));
}
}
if (require.main === module) {
const oauthServer = new OAuthServer();
oauthServer.start();
}
module.exports = { OAuthServer };