7

This is rather strange. I used Google Drive API to create a folder in Google Drive and then uploaded a file there. I can retrieve the folder and file using the same API (the code is working fine in all respect). However, when I go to Google Drive Web interface, I can't seem to find the folder or file. The file also doesn't sync to my local drive. Is there a setting in API or elsewhere to set the "visibility" ON?

Thank you in advance.

Allen King
  • 2,372
  • 4
  • 34
  • 52
  • are you sure you're logging in to the same account you created the file with? What does about.get() via the API show? I suspect you're either mixing accounts or using a service account... – Jay Lee Dec 12 '14 at 17:42
  • Let me check about.get() output. What is a service account? I just have one Google Drive account (that is same as my gmail account) – Allen King Dec 12 '14 at 17:46
  • You may try this answer, it definitely works fine: https://stackoverflow.com/a/67467388/7610978 – Kartik Arora May 10 '21 at 08:49

5 Answers5

4

I had the same issue. Turned out to be permissions. When the file is uploaded by the service account, the service account is set as the owner, and then you can't see the files from the Drive UI. I found the solution online (but can't seem to find it again...) This is what I did...

It's C#, your question didn't specify. The code you're interested in is the permission stuff after you get the response body after the upload...

FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();

//Start here...
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
Permission newPermission = new Permission();
newPermission.Value = "yourdriveaccount@domain.com";
newPermission.Type = "user";
newPermission.Role = "reader";
service.Permissions.Insert(newPermission, file.Id).Execute();

The file was visible on the Drive UI after this. I tried specifying "owner" for the role, like the api suggests, but I got and error saying that they're working on it. I haven't played around with the other setting yet, (I literary did this last night). Let me know if you have any luck with any other combinations on permissions.

Hope that helps

Spock
  • 4,700
  • 2
  • 16
  • 21
  • Actually, files are not being uploaded using a service account. – Allen King Dec 20 '14 at 18:57
  • Thanks! I was doing this in ruby and faced exact problem. [This link](https://developers.google.com/drive/api/v3/reference/permissions/create) and your answer helped me. – Omkar Aug 29 '20 at 22:07
  • 2
    I had the same issue for nodejs. Added permission for my own account and it started showing up under "Shared with me" in the google drive. – Meliodas Oct 11 '20 at 08:17
3

I had the same issue, but this got solved my using a list data type for parents parameter, eg: If one wants to create a folder under a folder("1TBymLMZXPGkouw-lTQ0EccN0CMb_yxUB") then the python code would look something like

drive_service = build('drive', 'v3', credentials=creds)
body={
            'name':'generated_folder',
            'parents':['1TBymLMZXPGkouw-lTQ0EccN0CMb_yxUB'],
            'mimeType':'application/vnd.google-apps.folder'
    }
doc = drive_service.files().create(body=body).execute()
Pavan Varyani
  • 1,454
  • 1
  • 8
  • 15
1

While permission issue is the main cause of this problem. What I did to make the folders or files appear after I uploaded it with service account was to specify the parent folder. If you upload / create folder / files without parent folder ID, that object's owner will be the service account that you are using.
By specifying parent ID, it will use the inherited permissions.
Here's the code I use in php (google/apiclient)

$driveFile = new Google\Service\Drive\DriveFile();
$driveFile->name = $req->name;
$driveFile->mimeType = 'application/vnd.google-apps.folder';
$driveFile->parents = ['17SqMne7a27sKVviHcwPn87epV7vOwLko'];
$result = $service->files->create($driveFile);
Irfandi D. Vendy
  • 894
  • 12
  • 20
0

When you create the folder, you should ensure you set a parent, such as 'root'. Without this, it will be not appear in 'My Drive' and only in Search (Have you tried searching in the UI?)

Since you have already created the folder, you can update the file and give it the parent root as well.

You can test it out using the Parents insert 'Try it now' example.

Put your Folders ID in the fileId box, then in the request body, add root in the ID field.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
  • I have been setting parents properly. I create a folder under root and then upload file to that folder. Read works ok going through the same folder structure. – Allen King Dec 20 '14 at 18:52
0
private void SetFilePermission(string fileId)
    {
        Permission adminPermission = new Permission
        {
            EmailAddress = "test@gmail.com", // email address of drive where 
                                                //you want to see files
            Type = "user",
            Role = "owner"
        };
        var permissionRequest = _driveService.Permissions.Create(adminPermission, fileId);
        permissionRequest.TransferOwnership = true;   // to make owner (important)
        permissionRequest.Execute();
        Permission globalPermission = new Permission
        {
            Type = "anyone",
            Role = "reader"
        };
        var globalpermissionRequest = _driveService.Permissions.Create(globalPermission, fileId);
        globalpermissionRequest.Execute();
    }
Nadeem
  • 31
  • 1
  • 4