英文:
Unable to list folders in GCP using go client library
问题
我正在阅读cloudresourcemanager
包的文档,并尝试构建一个简单的示例来列出我的GCP项目的文件夹。
然而,以下示例失败了:
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v2"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
fmt.Println(foldersService)
foldersListCall := foldersService.List()
resp, err := foldersListCall.Do()
if err != nil {
fmt.Println("Here")
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.Name)
}
}
它在以下代码处失败:
resp, err := foldersListCall.Do()
错误信息为:
googleapi: Error 400: Request contains an invalid argument., badRequest
我设置了以下环境变量:
GOOGLE_CLOUD_PROJECT=my-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/application_default_credentials.json
并且gcloud
命令行工具正常工作。
有什么建议吗?我可能漏掉了什么吗?
英文:
I am going through the documentation of cloudresourcemanager
package and trying to build a simple example to list the folders of my GCP project.
The following example however fails
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v2"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
fmt.Println(foldersService)
foldersListCall := foldersService.List()
resp, err := foldersListCall.Do()
if err != nil {
fmt.Println("Here")
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.Name)
}
}
It fails in
resp, err := foldersListCall.Do()
and the error is
googleapi: Error 400: Request contains an invalid argument., badRequest
I have the following environment variables set
GOOGLE_CLOUD_PROJECT=my-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/application_default_credentials.json
and gcloud
cli works fine.
Any suggestions what I might be missing?
答案1
得分: 2
错误消息一点帮助都没有...
问题是我没有在请求中设置Parent
参数,即organization
(切换到v3
有所帮助)
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
foldersListCall := foldersService.List()
foldersListCall.Parent("organizations/12345678910")
resp, err := foldersListCall.Do()
if err != nil {
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.DisplayName)
}
}
英文:
The error message is not helpful at all...
The problem is I was not setting the Parent
parameter in the request, i.e. the organization
(switching to v3
helped a bit)
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
foldersListCall := foldersService.List()
foldersListCall.Parent("organizations/12345678910")
resp, err := foldersListCall.Do()
if err != nil {
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.DisplayName)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论