Search code examples
gogoogle-drive-apibackupbackend

How to get a list of all files in directory with Google Drive API(v3)


I have stuck with function that must to return me a list of all files from directory (in this case directory is "root"). When I call this function, it return me files that only I added with my program (this program also can upload files to Google Drive), not all files. And it also shows me files that I deleted :/. What I do wrong? This function I was copied from Google Drive API Quickstart

service, err := getService()
    if err != nil {
        log.Fatalf("Unable to retrieve Drive client: %v", err)
    }

    r, err := service.Files.List().Q("'root' in parents").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve files: %v", err)
    }
    fmt.Println("Files:")
    if len(r.Files) == 0 {
        fmt.Println("No files found.")
    } else {
        for _, i := range r.Files {
            fmt.Printf("%v (%vs )\n", i.Name, i.Id)
        }
    }

Solution

    • You want to retrieve all files just under the root folder.
    • You want to achieve this using google-api-go-client with golang.
    • You have already been get and put values for Google Drive using Drive API.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    Issue and workaround:

    From the situation of When I call this function, it return me files that only I added with my program (this program also can upload files to Google Drive), not all files., I thought that your scopes might include https://www.googleapis.com/auth/drive.file. When https://www.googleapis.com/auth/drive.file is used as the scope, only the files created by the application are retrieved.

    In order to retrieve all files just under the root folder, please use the following scopes.

    • https://www.googleapis.com/auth/drive
    • https://www.googleapis.com/auth/drive.readonly
    • https://www.googleapis.com/auth/drive.metadata.readonly
    • https://www.googleapis.com/auth/drive.metadata.

    If you want to retrieve only the file list, the scopes of .readonly can be used.

    Modified script:

    From your question, I could notice that you are using google-api-go-client with golang and Go Quickstart. In this case, how about the following modification?

    If drive.DriveFileScope is included in the scopes, please modify as follows.

    From:
    config, err := google.ConfigFromJSON(b, drive.DriveFileScope)
    
    To:
    config, err := google.ConfigFromJSON(b, drive.DriveMetadataScope)
    

    or

    config, err := google.ConfigFromJSON(b, drive.DriveReadonlyScope)
    
    • If you want to also upload the file, please use drive.DriveScope.

    Note:

    • When you modified the scopes, please remove the file of token.json of tokFile := "token.json". And please run the script and authorize again. By this, the modified scopes are reflected to the access token and refresh token. Please be careful this.

    References:

    If I misunderstood your question and this was not the direction you want, I apologize.