Skip to content

Instantly share code, notes, and snippets.

@rainabba
Created December 30, 2015 21:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rainabba/065108d71b953c19843a to your computer and use it in GitHub Desktop.
Save rainabba/065108d71b953c19843a to your computer and use it in GitHub Desktop.
VERY basic example of how to save content to Google Drive using node.js using googleapis
// This requires a google developers console project. That UI changes regularly and I found it hard to learn, but at the time I'm creating this, the steps are:
// - At the top-right, select or create a new project (can't give step-by-step details here)
// - Visit https://console.developers.google.com/apis/api/drive/overview
// - Enable API with your options (not sure they matter much, but API must be enabled)
// - Visit https://console.developers.google.com/permissions/serviceaccounts
// - Create a new service account if you don't already have one or don't have one you want to use for these API calls. Select the "Furnish new private key" option and the JSON format along with "Enable Google Apps Domain-wide Delegation".
// - Save provided JSON in a SECURE location (don't include in your source code and ensure it's ignored by GIT)
// You can generate new keys and/or revoke this one at https://console.developers.google.com/apis/credentials
var gclient = require("googleapis"),
//YOU must create this file to return the appropriate object or update the gconfig values below to hardcode (not recommended for security).
gconfig = {
CLIENT_ID: 'yourserviceaccountname@your-project-name-01.iam.gserviceaccount.com',
SERVICE_EMAIL: 'yourGoogleAccount@someTld.com', //
JSON_FILE_PATH: './yourServiceAccountKeyFile.json' //Key file provided from setup above
},
jwtClient = new gclient.auth.JWT(
gconfig.CLIENT_ID,
gconfig.JSON_FILE_PATH,
null, ['https://www.googleapis.com/auth/drive',
// 'https://www.googleapis.com/auth/drive.appdata',
// 'https://www.googleapis.com/auth/drive.apps.readonly',
// 'https://www.googleapis.com/auth/drive.file',
// 'https://www.googleapis.com/auth/drive.metadata',
// 'https://www.googleapis.com/auth/drive.metadata.readonly',
// 'https://www.googleapis.com/auth/drive.readonly',
// 'https://www.googleapis.com/auth/drive.scripts',
'https://www.googleapis.com/auth/admin.reports.audit.readonly'
],
gconfig.SERVICE_EMAIL
); // I think for an insert, this doesn't actually control the owner but it's required and I'm fuzzy on the purpose
function insertTest(drive) { //Need to figure out how to specify owner because the service accont owns this file right now which isn't very useful
drive.files.insert({
resource: {
title: 'I was created by Node.js!',
mimeType: 'text/plain'
},
media: {
mimeType: 'text/plain',
body: 'Ipsum Lorem!'
}
},
function(err, resp) {
if (err) {
console.log('insert error: ', err);
} else {
console.log('File created. See id following:');
console.dir(resp);
}
}
);
}
jwtClient.authorize(function(err, tokens) {
if (err) {
console.log("Error authorizing with JWT", err);
return;
}
var drive = gclient.drive({
version: 'v2',
auth: jwtClient
});
insertTest(drive);
});
@ayinlaaji
Copy link

I find google apis cumbersome to go through :(

@ayinlaaji
Copy link

Thank you for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment