forked from IonicaBizau/test-youtube-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
168 lines (137 loc) · 5.16 KB
/
index.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Dependencies
var Youtube = require("youtube-api")
, Http = require("http")
, Statique = require ("statique")
, Http = require("http")
, Request = require("request")
, credentials = require("./credentials")
, path = require("path")
;
// Set ACCESS_TOKEN global as undefined
global.ACCESS_TOKEN = undefined;
// Credentials
credentials.scope = "https://www.googleapis.com/auth/youtube";
credentials.response_type = "code";
credentials.access_type = "offline";
// statique config
Statique
.server({root: __dirname + "/public"})
.setRoutes({
"/": function rootPage(req, res) {
var authType = credentials.auth_uri ? "oauth" : "jwt";
// Handle JWT authentication
if (authType === "jwt" && !ACCESS_TOKEN) {
ACCESS_TOKEN = true;
return Youtube.authenticate({
type: "jwt"
, email: credentials.email
, keyFile: path.normalize(credentials.keyFile)
, key: credentials.key
, subject: credentials.subject
, scopes: [credentials.scope]
}).authorize(function (err, data) {
if (err) { return Statique.error(req, res, 500, err.toString()); }
rootPage(req, res);
});
}
if (ACCESS_TOKEN) {
return Statique.readFile("/html/index.html", function (err, content) {
Statique.sendRes(res, 400, "text/html", content);
});
}
var authUrl = "https://accounts.google.com/o/oauth2/auth?";
for (var key in credentials) {
console.log(key, credentials[key]);
if (key === "client_secret") { continue; }
authUrl += "&" + key + "=" + credentials[key];
}
res.writeHead(302, {
"Location": authUrl
});
res.end();
return;
}
, "/api/run_code": function (req, res) {
var formData = ""
, error = ""
;
req.on("data", function (data) {
formData += data;
});
req.on("error", function (data) {
error += data;
});
req.on("end", function (data) {
if (error) {
return Statique.sendRes(res, 400, "text/html", error);
}
global.__api_run_code_callback = function (err, data) {
if (err) {
return Statique.sendRes(res, 400, "text", JSON.stringify(err));
}
return Statique.sendRes(res, 200, "text/json", JSON.stringify(data, null, 2));
};
formData = formData.replace(/_CALLBACK/g, "__api_run_code_callback");
try {
eval(formData);
} catch (e) {
return Statique.sendRes(res, 400, "text", e.message);
}
});
}
, "/oauth2callback": function (req, res) {
var url = req.url;
if (url.indexOf("error") !== -1) {
return res.end("Error.");
}
if (url.indexOf("?code=") === -1) {
return res.end("Invalid request.");
}
var code = url;
code = code.substring(code.indexOf("?code=") + 6);
if (!code) {
return res.end("Code is missing.");
}
var formData = "code=" + code +
"&client_id=" + credentials.client_id +
"&client_secret=" + credentials.client_secret +
"&redirect_uri=" + credentials.redirect_uri +
"&grant_type=authorization_code";
var options = {
url: "https://accounts.google.com/o/oauth2/token",
headers: {"content-type" : "application/x-www-form-urlencoded"},
method: "POST",
body: formData
};
Request(options, function (err, response, body) {
if (err) {
return res.end(err);
}
try {
body = JSON.parse(body);
} catch (e) {
return res.end(e.message + " :: " + body);
}
if (body.error) {
return res.end(err || body.error);
}
// success
if (body.access_token) {
ACCESS_TOKEN = body.access_token;
Youtube.authenticate({
type: "oauth",
token: ACCESS_TOKEN
});
res.writeHead(302, {
"Location": "/"
});
res.end();
}
return res.end("Something wrong: \n" + JSON.stringify(body, null, 4));
});
}
})
;
// Create server
Http.createServer(Statique.serve).listen(5000);
console.log("Open: http://localhost:5000");