我正在尝试使用服务帐户通过 api 连接到我的谷歌驱动器,但是当我尝试查询列表时,它返回空结果。
下面是我的代码:
func main() {
ctx := context.Background()
srv, err := drive.NewService(ctx, option.WithCredentialsFile("ServiceAccountCred.json"), option.WithScopes(drive.DriveScope))
if err != nil {
log.Fatalf("Warning: Unable to create drive Client %v", err)
}
r, err := srv.Files.List().PageSize(10).
Fields("nextPageToken, files(id, name)").Do()
if err != nil {
log.Fatalf("Unable to retrieve files: %v", err)
}
for _, res := range r.Files {
fmt.Println(res.Name)
}
}
我的ServiceAccountCred是直接通过Google Cloud上的服务帐户页面(添加密钥>下载)生成的,并且在创建DriveService时没有错误。我做错了什么?
服务帐户不是您,它有自己的 Google 云端硬盘帐户。
创建时,里面没有文件
好消息,你的代码完美运行😊