Last active
August 29, 2015 14:05
-
-
Save byzhang/7e9d33ba1d8581642ef8 to your computer and use it in GitHub Desktop.
first node server
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
var async = require('async'); | |
var express = require('express'), | |
app = express(); | |
var google = require('googleapis'), | |
OAuth2Client = google.auth.OAuth2; | |
var oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); | |
// Authorization uri definition | |
var authorization_uri = oauth2Client.generateAuthUrl({ | |
access_type: 'offline', // will return a refresh token | |
scope: SCOPE | |
}); | |
var drive = google.drive('v2'); | |
app.get('/auth', function (req, res) { | |
res.redirect(authorization_uri); | |
}); | |
function listImageMetadata(res) { | |
var metadata = []; | |
drive.files.list({ | |
q: "mimeType contains 'image/' or mimeType contains 'video/'", | |
auth: oauth2Client | |
}, function(err, response) { | |
console.log(err); | |
async.each(response.items, function(item, callback) { | |
drive.files.get({ | |
fileId: item.id, | |
auth: oauth2Client | |
}, function(err, response) { | |
console.log(response.imageMediaMetadata); | |
metadata[metadata.length] = response.imageMediaMetadata; | |
console.log(metadata.length); | |
callback(); | |
}); | |
}, function(err) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(metadata); | |
res.json(metadata); | |
} | |
}); | |
}); | |
} | |
// Callback service parsing the authorization token and asking for the access token | |
app.get('/oauth2callback', function (req, res) { | |
var code = req.query.code; | |
console.log(code); | |
// request access token | |
oauth2Client.getToken(code, function(err, tokens) { | |
// set tokens to the client | |
oauth2Client.setCredentials(tokens); | |
listImageMetadata(res); | |
}); | |
}); | |
app.get('/', function (req, res) { | |
// TODO: check and load oauth2Client from somewhere. If invalid then /auth. | |
res.redirect(authorization_uri); | |
}); | |
app.listen(8081); | |
console.log('Express server started on port 8081'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment