The solution is to create the document, then share it with your own account so that you can view the documents in Drive. However, when I first tried this I ran into several issues with the API not allowing me to transfer ownership. Also, I was not able to change permissions on a document that had just been created. So instead I was able to grant Edit permissions and I had to implement retry logic on the permissions call.
var googleapis = require('googleapis').google;
var googleauth = require('google-auth-library');
var fs = require('fs');
var credentials = require('./credentials/google.json');
var Google = {};
Google.authorize = function() {
return new Promise(function(resolve, reject) {
var client = new googleauth.JWT({
email: credentials.client_email,
key: credentials.private_key,
scopes: ['https://www.googleapis.com/auth/drive.file']
});
client.authorize(function(err, result) {
if (err) {
console.log("Google.authorize "+err);
reject();
} else {
resolve(client);
}
});
});
};
Google.getDrive = function(auth) {
return googleapis.drive({
version: 'v3',
auth: auth
});
};
Google.createFile = function(drive, fileName, mimeType, stream) {
return new Promise(function(resolve, reject) {
drive.files.create({
requestBody: {
name: fileName,
mimeType: mimeType
},
media: {
mimeType: mimeType,
body: stream
}
}, null, function (err, result) {
if (err) {
console.log("Google.createFile "+err);
reject();
} else {
var retry = function(id, waitTime) {
if (waitTime > 10000) {
reject();
}
setTimeout(function() {
drive.permissions.create({
fileId: id,
resource: {
role: "writer",
type: "user",
emailAddress: "YOUR-EMAIL-HERE"
}
}, {
}, function(err, result) {
if (err) {
console.log("Drive Permissions - Retrying after waiting "+waitTime);
retry(id, waitTime*2);
} else {
resolve(id);
}
});
}, waitTime);
};
var id = result.data.id;
console.log("Uploaded ID " + id);
retry(id, 1000);
}
});
});
};
module.exports = Google;