I can't see the files and folders created via code in my Google Drive

后端 未结 4 1335
遥遥无期
遥遥无期 2020-12-03 19:37

I\'m a web developer and i\'d like to access to the Google APIs from my business software, I\'ve created a Drive account and I\'d to create a folders and files via code (VB.

4条回答
  •  感情败类
    2020-12-03 20:20

    Was not aware of the given solution. I did differently by adding a file permission, after having added the file (NodeJS code. insertFile is called after a classic fileUpload by the user) :

    insertPermission = function(fileId, value, type, role,authClient,cb) {
      var body = {
        'value': value,
        'type': type,
        'role': role
      };
      var request = drive.permissions.insert({
        'fileId': fileId,
        'resource': body,
        'auth': authClient
      }, function (err,response) {
        cb(err,response);
      });
    }
    
    exports.insertFile = function(file,customer,callback) {
        exports.authorize(function (err,authClient) {
            drive.files.insert({
              resource: {
                title: file.name,
                mimeType: file.mimeType,
                parents: [{id:customer.googleDriveFolder}]
              },
              media: {
                mimeType: file.mimeType,
                body: file.buffer
              },
              auth: authClient
            }, function(err, response) {
              debug('error:', err, 'inserted:', response.id);
              if (!err) {
                insertPermission(response.id,customer.email,"user","writer",authClient,function (err,resp){
                  callback(null, resp);            
                });
              }
            });
        });
    };
    

    Note that i'm using JWT with private key kind of authentication, with a service account :

    exports.authorize = function(callback) {
        if (_tokens && ((_tokens.expiry_date - (new Date()).getTime())>100 )) callback(null, _authClient);
        else {
    
        var scope = ['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'];
    
        var authClient = new google.auth.JWT(
                '',
                '.pem',
                ''); //seems we could use an user to impersonate requests, i need to test that..
        authClient.authorize(function(err, tokens) {
            if (err) {
                callback(err);
                return;
            }
            callback(null,authClient);    
        });
        }
    };
    

提交回复
热议问题