我坚持使用必须向我返回目录中所有文件的列表的功能(在这种情况下,目录为“root”)。当我调用此函数时,它将返回仅是我在程序中添加的文件(该程序还可以将文件上传到Google云端硬盘),而不是所有文件。它还显示了我删除的文件:/。我做错了什么?
此功能是我从Google Drive API快速入门复制而来的
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)
}
}
最佳答案
如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。
问题和解决方法:
从
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.
的情况来看,我认为您的范围可能包括https://www.googleapis.com/auth/drive.file
。当https://www.googleapis.com/auth/drive.file
用作范围时,仅检索由应用程序创建的文件。为了检索根文件夹下的所有文件,请使用以下范围。
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
。 如果只想检索文件列表,则可以使用
.readonly
的范围。修改后的脚本:
从您的问题中,我可以注意到您正在将google-api-go-client与golang和Go Quickstart结合使用。在这种情况下,如何进行以下修改?
如果范围内包括
drive.DriveFileScope
,请进行以下修改。从:
config, err := google.ConfigFromJSON(b, drive.DriveFileScope)
到:
config, err := google.ConfigFromJSON(b, drive.DriveMetadataScope)
或者
config, err := google.ConfigFromJSON(b, drive.DriveReadonlyScope)
drive.DriveScope
。 笔记:
token.json
的tokFile := "token.json"
文件。并且请运行脚本并再次授权。这样,修改后的范围将反射(reflect)到访问 token 和刷新 token 中。请注意这一点。 引用:
如果我误解了您的问题,而这不是您想要的方向,我深表歉意。
关于go - 如何使用Google云端硬盘API(v3)获取目录中所有文件的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60192379/