0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
James Watkins
  • 4,806
  • 5
  • 32
  • 42
  • Seems you are using a service account and that is where the documents are being stored. – Morfinismo Jun 16 '18 at 23:30
  • Still, how do I view/edit the documents? I cannot login with the service account because there is no password for it. It only has API keys. – James Watkins Jun 17 '18 at 16:03
  • Related Question: https://stackoverflow.com/questions/30023061/google-service-account-user-interfece – James Watkins Jun 17 '18 at 16:11
  • https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention/19766913#19766913 – pinoyyid Jul 01 '18 at 21:34
  • @pinoyyid I'm specifically asking about JWT authentication (service account), not oauth. – James Watkins Jul 02 '18 at 14:16
  • I know. I'm suggesting an alternative approach to your requirement that avoids the issues you're facing. – pinoyyid Jul 02 '18 at 15:45
  • It's not really an alternative, it's a hackish workaround that is fragile and will randomly stop working without warning and require manual intervention to fix. – James Watkins Jul 03 '18 at 17:42

1 Answers1

1

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;
James Watkins
  • 4,806
  • 5
  • 32
  • 42