-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
index.ts
131 lines (98 loc) · 3.54 KB
/
index.ts
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
import express from 'express';
import { Innertube, UniversalCache } from 'youtubei.js';
import { OAuth2Client } from 'google-auth-library';
const app = express();
let innertube: Innertube | undefined;
let oAuth2Client: OAuth2Client | undefined;
/**
* To get your own client id and secret, visit https://console.developers.google.com/, create a new project,
* and create an OAuth 2.0 Client ID (Web application) under the Credentials tab.
*
* Don't forget to add http://localhost:3000/login as an authorized redirect URI.
*/
const clientId = 'YOUR_OAUTH2_CLIENT_ID';
const clientSecret = 'YOUR_OAUTH2_CLIENT_SECRET';
const redirectUri = 'http://localhost:3000/login';
const port = 3000;
let authorizationUrl: string | undefined;
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true, limit: '3mb' }))
const cache = new UniversalCache(true);
console.info("Cache dir:", cache.cache_dir);
app.get('/', async (_req, res) => {
if (!innertube) {
console.info('Creating innertube instance.');
innertube = await Innertube.create({ cache });
innertube.session.on("update-credentials", async (_credentials) => {
console.info('Credentials updated.');
await innertube?.session.oauth.cacheCredentials();
});
}
if (await cache.get('youtubei_oauth_credentials'))
await innertube.session.signIn();
if (innertube.session.logged_in) {
console.info('Innertube instance is logged in.');
const userInfo = await innertube.account.getInfo();
console.log(await innertube.getBasicInfo('R8vgwMYSQi8', 'ANDROID'));
return res.send({ userInfo });
}
if (!oAuth2Client) {
console.info('Creating OAuth2 client.');
oAuth2Client = new OAuth2Client(
clientId,
clientSecret,
redirectUri
);
authorizationUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: [
"http://gdata.youtube.com",
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.force-ssl",
"https://www.googleapis.com/auth/youtube-paid-content"
],
include_granted_scopes: true,
prompt: 'consent',
});
console.info('Redirecting to authorization URL...');
res.redirect(authorizationUrl);
} else if (authorizationUrl) {
console.info('OAuth2 client already exists. Redirecting to authorization URL...');
res.redirect(authorizationUrl);
}
});
app.get('/login', async (req, res) => {
const { code } = req.query;
if (!code) {
return res.send('No code provided.');
}
if (!oAuth2Client || !innertube) {
return res.send('OAuth2 client or innertube instance is not initialized.');
}
const { tokens } = await oAuth2Client.getToken(code as string);
if (tokens.access_token && tokens.refresh_token && tokens.expiry_date) {
await innertube.session.signIn({
access_token: tokens.access_token,
refresh_token: tokens.refresh_token,
expiry_date: new Date(tokens.expiry_date).toISOString(),
client: {
client_id: clientId,
client_secret: clientSecret
}
});
await innertube.session.oauth.cacheCredentials();
console.log('Logged in successfully. Redirecting to home page...');
res.redirect('/');
}
});
app.get('/logout', async (_req, res) => {
if (!innertube) {
return res.send('Innertube instance is not initialized.');
}
await innertube.session.signOut();
console.log('Logged out successfully. Redirecting to home page...');
res.redirect('/');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});