/
youtube.js
89 lines (77 loc) · 2.46 KB
/
youtube.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
var express = require('express'),
request = require('request'),
query = require('querystring'),
args = require('optimist').
options('key', {alias: 'k'}).
options('secret', {alias: 's'}).
options('host', {alias:'h'}).
argv;
var options = {
url: 'http://www.googleapis.com/youtube/v3/videos',
method: 'POST',
auth: {
base_url: 'https://accounts.google.com/o/',
access_name: 'access_token',
authorize_url: 'oauth2/auth',
access_url: 'oauth2/token',
grant_type: 'authorization_code',
scope: 'https://www.googleapis.com/auth/youtube'
},
form: {}
};
if (!args.key)
throw new Error('Missing Consumer Key! -k cli option');
if (!args.secret)
throw new Error('Missing Consumer Secret! -s cli option');
if (!args.host)
throw new Error('Missing Host IP (Should be public ip / domain not localhost)!');
// Application Details for Auth Leg
var app = express();
app.configure(function () {
app.set('view engine', 'ejs');
app.set('views', __dirname + '/_views');
app.use(express.static(__dirname + '/_assets'));
app.use(express.bodyParser());
app.use(express.cookieParser('maeby, lets keep it a secret?'));
app.use(express.session({ secret: 'no-more-secrets' }));
});
app.get('/', function (req, res) {
request({
method: 'POST',
uri: 'http://' + args.host + ':3000/store',
form: {
client_id: args.key,
client_secret: args.secret,
base_url: options.auth.base_url,
grant_type: options.auth.grant_type,
access_url: options.auth.access_url,
request_url: options.auth.request_url,
authorize_url: options.auth.authorize_url,
access_name: options.auth.access_name,
scope: options.auth.scope,
auth_type: "oauth",
auth_version: 2,
auth_leg: 3,
oauth_token: "",
callback: "http://" + args.host + ":3001/callback",
redirect: "http://" + args.host + ":3000/callback"
}
}, function (error, response, body) {
if (error) return console.log(error);
body = JSON.parse(body);
res.send('<a href="http://' + args.host + ':3000/start?hash=' + body.hash + '">Start Process</a>');
});
});
app.get('/callback', function (req, res) {
request({
method: 'GET',
uri: options.url + '?access_token=' + req.query.access_token,
headers: {
'User-Agent': 'Gatekeeper'
}
}, function (error, response, body) {
res.json(JSON.parse(body));
});
});
app.listen(3001);
console.log('app started at: http://' + args.host + ':3001');