Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive Folders/Files Created Using API Not Visible on Google Interface

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.

like image 228
Allen King Avatar asked Dec 12 '14 17:12

Allen King


3 Answers

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 = "[email protected]";
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

like image 197
Spock Avatar answered Sep 20 '22 01:09

Spock


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()
like image 27
Pavan Varyani Avatar answered Sep 22 '22 01:09

Pavan Varyani


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);
like image 21
Irfandi D. Vendy Avatar answered Sep 18 '22 01:09

Irfandi D. Vendy