forked from IonicaBizau/test-youtube-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request IonicaBizau#2 from IonicaBizau/statique
Use Statique NPM module
- Loading branch information
Showing
289 changed files
with
206,225 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,145 @@ | ||
var Youtube = require("youtube-api"); | ||
var http = require("http"); | ||
|
||
var express = require('express'), | ||
yt = require('./routes/yt'); | ||
|
||
var app = express(); | ||
var Youtube = require("youtube-api") | ||
, Http = require("http") | ||
, Statique = require ("statique") | ||
, Http = require('http') | ||
, Request = require("request") | ||
, credentials = require("./credentials") | ||
; | ||
|
||
global.ACCESS_TOKEN = undefined; | ||
|
||
app.get('/yt/activities/:maxResults', yt.listAllActivities); | ||
app.get('/yt/activities', yt.listAllActivities); | ||
app.get('/yt/activities/after/:date', yt.listActivitiesAfter); //with this format YYYY-MM-DDThh:mm:ss.sZ | ||
app.get('/yt/activities/before/:date', yt.listActivitiesBefore); //with this format YYYY-MM-DDThh:mm:ss.sZ | ||
|
||
// You have to provide the credentials, first (in credentials.json file: rename .templ.json into json) | ||
var credentials = require("./credentials"); | ||
var request = require("request"); | ||
|
||
// credentials | ||
credentials.scope = "https://www.googleapis.com/auth/youtube"; | ||
credentials.response_type = "code"; | ||
credentials.access_type = "offline"; | ||
|
||
app.get('/',function(req, res){ | ||
|
||
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]; | ||
} | ||
if (typeof ACCESS_TOKEN == "undefined") { | ||
var html = "Click <a href='" + authUrl + "'>here</a> to get the access token."; | ||
} else { | ||
var html = "You can call this REStfull links:"; | ||
html += "<br><ul><li>/yt/activities = All activities limited to 50 results.</li>"; | ||
html += "<li>/yt/activities/15 = All activities limited to 15 results.</li>"; | ||
html += "<li>/yt/activities/after/YYYY-MM-DDThh:mm:ss.sZ = All activities limited to 50 result that where published after the date expecified.</li>"; | ||
html += "<li>/yt/activities/before/YYYY-MM-DDThh:mm:ss.sZ = All activities limited to 50 result that where published before the date expecified.</li></ul>"; | ||
html += "<br>Click <a href='/yt/activities'>here</a> to see the list of all activities. (/yt/activities)"; | ||
} | ||
res.setHeader("Content-Type", "text/html"); | ||
res.end(html); | ||
return; | ||
}); | ||
|
||
app.get('/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); | ||
// statique config | ||
Statique | ||
.server({root: __dirname + "/public"}) | ||
.setRoutes({ | ||
"/": function (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; | ||
} | ||
if (body.error) { | ||
return res.end(err || body.error); | ||
, "/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", "__api_run_code_callback"); | ||
try { | ||
eval(formData); | ||
} catch (e) { | ||
return Statique.sendRes(res, 400, "text", e.message); | ||
} | ||
}); | ||
} | ||
|
||
// success | ||
if (body.access_token) { | ||
ACCESS_TOKEN = body.access_token; | ||
var html = "Click <a href='/'>here</a> to go back.</br> Access token: " + ACCESS_TOKEN; | ||
res.setHeader("Content-Type", "text/html"); | ||
return res.end(html); | ||
, "/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; | ||
res.writeHead(302, { | ||
"Location": "/" | ||
}); | ||
res.end(); | ||
} | ||
|
||
return res.end("Something wrong: \n" + JSON.stringify(body, null, 4)); | ||
}); | ||
} | ||
}) | ||
; | ||
|
||
return res.end("Something wrong: \n" + JSON.stringify(body, null, 4)); | ||
}); | ||
|
||
return; | ||
}); | ||
|
||
app.listen(5000); | ||
// create server | ||
Http.createServer(function(req, res) { | ||
Statique.serve (req, res); | ||
}).listen(5000); | ||
|
||
// output | ||
console.log("Open: http://localhost:5000"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.