From 6e4cf4e480fd5a906b8a6567d3577236d2f81490 Mon Sep 17 00:00:00 2001 From: Fernando Derkoski Date: Thu, 24 Apr 2014 10:04:29 -0300 Subject: [PATCH] Add express,routes --- index.js | 188 ++++++++++++++++++++++------------------------- package.json | 7 +- routes/videos.js | 18 +++++ 3 files changed, 110 insertions(+), 103 deletions(-) create mode 100644 routes/videos.js diff --git a/index.js b/index.js index 5002de0..6e59220 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,10 @@ -var Youtube = require("youtube-api"); -for (var i in Youtube) { - console.log(i); -} -var http = require("http"); +var express = require('express'), + videos = require('./routes/videos'); + +var app = express(); + +app.get('/videos', videos.listAllActivities); + // You have to provide the credentials, first (in credentials.json file: rename .templ.json into json) var credentials = require("./credentials"); var request = require("request"); @@ -11,100 +13,86 @@ credentials.scope = "https://www.googleapis.com/auth/youtube"; credentials.response_type = "code"; credentials.access_type = "offline"; -http.createServer(function (req, res) { - - if (req.url === "/") { - if (typeof ACCESS_TOKEN !== "undefined") { - Youtube.authenticate({ - type: "oauth", - token: ACCESS_TOKEN - }); - - Youtube.activities.list({"part": "snippet,contentDetails", "home":true, "maxResults": 50}, function (err, data) { - var response = ""; - response += "Error: " + JSON.stringify(err, null, 4) + "\n"; - response += "Data: " + JSON.stringify(data, null, 4); - res.end(response); - }); - - return; - } - - 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]; - } - - var html = "Click here to get the access token."; - res.setHeader("Content-Type", "text/html"); - res.end(html); - return; - } - - if (req.url.indexOf("oauth2callback") !== -1) { - - 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); - } - - // success - if (body.access_token) { - ACCESS_TOKEN = body.access_token; - return res.end("Access token: " + ACCESS_TOKEN); - } - - return res.end("Something wrong: \n" + JSON.stringify(body, null, 4)); - }); - - return; - } - - res.end("404 - Not found."); -}).listen(3000); +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 here to get the access token."; + } else { + var html = "Click here to see the list of all 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); + } + if (body.error) { + return res.end(err); + } + + // success + if (body.access_token) { + ACCESS_TOKEN = body.access_token; + var html = "Click here to go back.
Access token: " + ACCESS_TOKEN; + res.setHeader("Content-Type", "text/html"); + return res.end(html); + } + + return res.end("Something wrong: \n" + JSON.stringify(body, null, 4)); + }); + + return; +}); + +app.listen(3000); console.log("Open: http://localhost:3000"); diff --git a/package.json b/package.json index ec693fe..e2abea1 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "test-youtube-api", - "version": "0.1.0", + "version": "0.1.1", "description": "Test Youtube API NodeJS module", - "author": "IonicaBizau", + "author": "IonicaBizau,brilvio", "dependencies": { "youtube-api": "*", - "request": "*" + "request": "*", + "express": "*" }, "license": "BSD-2-Clause" } diff --git a/routes/videos.js b/routes/videos.js new file mode 100644 index 0000000..268ba27 --- /dev/null +++ b/routes/videos.js @@ -0,0 +1,18 @@ +var Youtube = require("youtube-api"); +for (var i in Youtube) { + console.log(i); +} + +exports.listAllActivities = function(req, res) { + Youtube.authenticate({ + type: "oauth", + token: ACCESS_TOKEN + }); + + Youtube.activities.list({"part": "snippet,contentDetails", "home":true, "maxResults": 50}, function (err, data) { + var response = ""; + response += "Error: " + JSON.stringify(err, null, 4) + "\n"; + response += "Data: " + JSON.stringify(data, null, 4); + res.end(response); + }); +}; \ No newline at end of file