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

后端 未结 4 1315
遥遥无期
遥遥无期 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:17

    You can see your files uploaded using API. Use follwing URL to see all your files.

    https://drive.google.com/folderview?id=XXX

    Where XXX= Folder ID created using API. Debug code to get folder id,

    But to work above link you have to provide permission after creating any folder or file as below:

     Permission newPermission = new Permission();
     newPermission.Value = "youremail@gmail.com";
     newPermission.Type = "anyone";
     newPermission.Role = "reader";
     _service.Permissions.Insert(newPermission, NewDirectory.Id).Execute();
    

    Alternate Link to See UI : https://drive.google.com/drive/folders/XXX

    0 讨论(0)
  • 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(
                '<myServiceAccountClientId - xxx@developer.gserviceaccount.com>',
                '<path to key>.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);    
        });
        }
    };
    
    0 讨论(0)
  • 2020-12-03 20:26

    Since you are using a Service Account, all the folders and files will be created in this Service Account's Drive which cannot be accessed through a web UI and will be limited to the default quota.

    To add content in a user's Drive, you will need to go through the regular OAuth 2.0 flow to retrieve credentials from this user. You can find more information about OAuth 2.0 on this pages:

    • Retrieve and use OAuth 2.0 credentials.
    • Quickstart: it has a quickstart sample in C# that you could use.
    • Using OAuth 2.0 to access Google APIs
    0 讨论(0)
  • 2020-12-03 20:26

    If you would like to see the files under a User Account without using OAuth 2.0, then you can give the Service account edit permission to a folder under the User Account.

    Then when uploading the file using the Service Account make sure you set Parent Reference ID to use that folder ID

    The folder ID is determined in the address bar

    Hope this helps.

    0 讨论(0)
提交回复
热议问题