Skip to content

Commit

Permalink
Add express,routes
Browse files Browse the repository at this point in the history
  • Loading branch information
brilvio committed Apr 24, 2014
1 parent f1ad2ac commit 6e4cf4e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 103 deletions.
188 changes: 88 additions & 100 deletions index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 <a href='" + authUrl + "'>here</a> 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 <a href='" + authUrl + "'>here</a> to get the access token.";
} else {
var html = "Click <a href='http://localhost:3000/videos'>here</a> 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 <a href='http://localhost:3000'>here</a> to go back.</br> 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");
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
18 changes: 18 additions & 0 deletions routes/videos.js
Original file line number Diff line number Diff line change
@@ -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);
});
};

0 comments on commit 6e4cf4e

Please sign in to comment.