Q:

Viewing documents uploaded to Drive using JWT in Node.js

I am building a web portal for users to upload documents that my team can view and edit in Google Drive. For this reason I am using JWT authentication for the upload.
I believe the code works because I am getting a successful response from the Google API. However I am not able to figure out where the documents are stored or how to view them in the Drive UI.
For example, I tried logging into Drive with the same account I used for generating the JWT credentials and the document is not visible.
Please help correct my understanding of how this Drive API integration works.

A:

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;

