Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created September 5, 2016 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/bdbd49c9f55f9b9311d26f6de7d4bd4c to your computer and use it in GitHub Desktop.
Save chuck0523/bdbd49c9f55f9b9311d26f6de7d4bd4c to your computer and use it in GitHub Desktop.
var readline = require('readline');
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var CLIENT_ID = 'YOUR CLIENT ID HERE',
CLIENT_SECRET = 'YOUR CLIENT SECRET HERE',
REDIRECT_URL = 'YOUR REDIRECT URL HERE',
SCOPE = 'https://www.googleapis.com/auth/drive.file';
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var auth = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var url = auth.generateAuthUrl({ scope: SCOPE });
var getAccessToken = function(code) {
auth.getToken(code, function(err, tokens) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
auth.credentials = tokens;
upload();
});
};
var upload = function() {
var drive = google.drive({ version: 'v2', auth: auth });
drive.files.insert({
resource: {
title: 'My Document',
mimeType: 'text/plain'
},
media: {
mimeType: 'text/plain',
body: 'Hello World!'
}
}, console.log);
};
console.log('Visit the url: ', url);
rl.question('Enter the code here:', getAccessToken);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment