Skip to content

Instantly share code, notes, and snippets.

@evnm
Created October 20, 2010 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evnm/636884 to your computer and use it in GitHub Desktop.
Save evnm/636884 to your computer and use it in GitHub Desktop.
Express and dropbox-node preliminaries
access_token = token_res['token'],
access_token_secret = token_res['secret'],
dropbox = new DropboxClient(oauth,
access_token,
access_token_secret);
res.redirect('/file_browser');
// Dropbox credential processing.
app.post('/process_creds', function(req, res) {
// Get access token.
var client = http.createClient(80, 'www.dropbox.com'),
request = client.request(
'GET', API_URI + API_VERSION +
'/token?oauth_consumer_key=' + consumer_key +
'&email=' + req.body.email +
'&password=' + req.body.password,
{'host': 'www.api.dropbox.com'});
request.end();
// Upon response, extract access token/secret and redirect to file_browser.
request.on('response', function(token_res) {
token_res.setEncoding('utf8');
token_res.on('data', function(chunk) {
var token_res = JSON.parse(chunk);
access_token = token_res['token'],
access_token_secret = token_res['secret'],
dropbox = new DropboxClient(oauth,
access_token,
access_token_secret);
res.redirect('/file_browser');
});
});
});
h1 #{title}
h2 #{current_dir}
ul#dir_contents
- items.forEach(function(item) {
li
- if(item.is_dir)
a(href: '/file_browser' + item.path) #{item.path}
- else
#{item.path}
- })
// File browser page.
app.get('/file_browser(/*)?', function(req, res) {
// Fetch target metadata and render the page.
dropbox.getMetadata(req.params[1] || '', function(err, metadata) {
if (err) console.log('Error: ' + sys.inspect(err));
else {
res.render('file_browser.jade', {
locals: {
title: 'Dropbox File Browser',
current_dir: (metadata.path.length > 0) ? metadata.path : 'root',
items: metadata.contents
}
});
}
});
});
html
head
title #{title}
link(rel: 'stylesheet', href: '/style.css')
body
div#wrapper
#{body}
h1 Enter your Dropbox credentialsa
form(method: 'post', action: '/process_creds')
div
label Email:
input(type: 'text', name: 'email', id:)
div
label Password:
input(type: 'password', name: 'password', id:)
div#editArticleSubmit
input(type: 'submit', value: 'Send')
// Login page.
app.get('/', function(req, res) {
res.render('login.jade', {
locals: {
title: 'Dropbox File Browser'
}
});
});
// Initialize OAuth object.
var API_URI = 'http://api.dropbox.com/',
API_VERSION = '0',
consumer_key = 'your_consumer_key',
consumer_secret = 'your_consumer_secret',
oauth = new OAuth(
API_URI + API_VERSION + '/oauth/request_token',
API_URI + API_VERSION + '/oauth/access_token',
consumer_key, consumer_secret,
'1.0', null, 'HMAC-SHA1');
// Set up persistent variables.
var access_token = '', access_token_secret = '', dropbox = null;
var sys = require('sys'),
http = require('http'),
OAuth = require('oauth').OAuth,
DropboxClient = require('dropbox').DropboxClient,
express = require('express'),
app = express.createServer();
app.configure(function() {
app.use(express.logger());
app.use(express.bodyDecoder());
});
body
:font-family "Helvetica", "Lucida Grande", "sans-serif'
:font-size 13px
:padding 0
div#wrapper
:margin 0 auto
:padding 25px
:width 500px
:border 3px solid #eee
h1
:text-shadow 1px 2px 2px #ddd
:font-size 24px
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment