chornbe
chornbe

Reputation: 1164

Shared a google drive folder with google service account, but service account can't see it

Writing an application that will crawl an uploads directory for packaged zip files uploaded by users, and recreate the in-zip directory structure in a Google for Work shared google drive folder.

Problem: The shared folder is not accessible or visible to the service account created in the For Work domain. Code is in C#. I hope I'm just missing something stupid.

Details: Logged in as the domain admin account. Go into the google api console, enable api, create a service account, grant access to that service account's email. Via API and service account, shared folder is not found in the list of File objects, nor can I upload to it by ID.

The following snippet is called using the in-url folder ID of the shared folder. The folder that is shared to the service account doesn't show up in the list of stuff returned.

// get starting point

    string masterParent = "xxxxx"; // pulled from URL after sharing folder
    File masterParentFolder = GetGAPIFileById( service, masterParent, false );

Example Snippet:

static File GetGAPIFileById( gapi.DriveService service, string id, bool folderOnly ) {
    if( null == id ) id = "-1";
    File rtn = null;

    var lreq = service.Files.List();
    lreq.MaxResults = 10000;
    if( folderOnly ) {
        lreq.Q = "mimeType='application/vnd.google-apps.folder' and trashed=false";
    } else {
        lreq.Q = "trashed=false";
    }
    var lres = lreq.Execute();

    foreach( File f in lres.Items ) {
        Console.WriteLine( "Item: " + f.Title + " // " + f.Id );
        if( f.Id.Trim().ToLower() == id.Trim().ToLower() ) {
            rtn = f;
            break;
        }
    }

    return rtn;
}

Upvotes: 6

Views: 3544

Answers (1)

chornbe
chornbe

Reputation: 1164

Well, as it turns out, it seems the issue was caused by, or exacerbated by changing the share-out-of-domain setting, and not waiting for full propagation before sharing the folder, and creating the service account. After deleting everything and redoing the exact same steps, roughly 5 days after the initial attempt, things are working properly. When Google says "this may take up to 24 hours" and it seems to work just fine within a few minutes, go ahead and leave the stuff alone and come back to it a day or two later.

:: shrug :: Thanks, all.

Upvotes: 4

Related Questions