-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
90 lines (72 loc) · 2.14 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
"use strict";
// Dependencies
var Youtube = require("youtube-api"),
Lien = require("lien"),
Logger = require("bug-killer"),
opn = require("opn"),
readJson = require("r-json"),
path = require("path"),
tryAsync = require("try-async");
var credentials = readJson(__dirname + "/credentials.json");
// Set ACCESS_TOKEN global as undefined
var CREDENTIALS = readJson(__dirname + "/credentials.json");
// Init lien server
var server = new Lien({
host: "localhost",
port: 5000,
public: __dirname + "/public"
});
server.on("load", function (err) {
if (err) throw err;
opn("http://localhost:5000");
});
// Authenticate
// You can access the Youtube resources via OAuth2 only.
// https://developers.google.com/youtube/v3/guides/moving_to_oauth#service_accounts
var oauth = Youtube.authenticate({
type: "oauth",
client_id: CREDENTIALS.web.client_id,
client_secret: CREDENTIALS.web.client_secret,
redirect_url: CREDENTIALS.web.redirect_uris[0]
});
var authDone = false;
// Handle oauth2 callback
server.addPage("/oauth2callback", function (lien) {
oauth.getToken(lien.query.code, function (err, tokens) {
if (err) {
lien.lien(err, 400);
return Logger.log(err);
}
oauth.setCredentials(tokens);
authDone = true;
lien.redirect("/");
});
});
server.addPage("/", function (lien) {
if (!authDone) {
return lien.redirect(oauth.generateAuthUrl({
access_type: "offline",
scope: ["https://www.googleapis.com/auth/youtube"]
}));
}
lien.file("html/index.html");
});
server.addPage("/api/run_code", "post", function (lien) {
if (!authDone) {
return lien.apiError("Please authenticate.", 403);
}
global.__api_run_code_callback = function (err, data) {
if (err) {
return lien.apiError(err);
}
return lien.end(data);
};
var formData = lien.data.code.replace(/_CALLBACK/g, "__api_run_code_callback");
tryAsync(function () {
eval(formData);
}, function (err) {
if (err) {
return lien.end(err, 400);
}
});
});