go - 在Go应用程序中使用默认凭据时出现GCP权限问题

标签 go google-cloud-platform

我的应用程序很小:

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/api/compute/v1"
)

func main() {
    ctx := context.Background()
    computeService, err := compute.NewService(ctx)
    urlMapCall := computeService.UrlMaps.List("my-prj")
    urlMaps, err := urlMapCall.Do()
    if err != nil {
        log.Println(err)
        return
    }
    for _, mp := range urlMaps.Items {
        fmt.Println(mp.Name)
    }
}

我得到错误:
googleapi: Error 403: Required 'compute.urlMaps.list' permission for 'projects/my-prj', forbidden

我设置了环境变量GOOGLE_APPLICATION_CREDENTIALS=$HOME/.gcp/my-prj-aef233we.json。此JSON表示的服务帐户具有“项目编辑者”和“计算管理员”角色。

当我执行gcloud compute url-maps list时,它可以正常工作。

我究竟做错了什么?

最佳答案

从GCP中,我还要求启用servceAccountUser角色

roles/ compute.admin Full control of all Compute Engine resources.

If the user will be managing virtual machine instances that are configured to run as a service account, you must also grant the roles/iam.serviceAccountUser role.

关于go - 在Go应用程序中使用默认凭据时出现GCP权限问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59125378/

相关文章:

go - ldap搜索过滤器以检查特定域下是否存在任何用户

arrays - 在 Golang 的整数范围 map 中查找

ssl - FTPS客户端Golang

go - 你将如何在 Go 中设置和清除单个位?

零停机时间的 Kubernetes 部署

go - "value semantics’ "和 "pointer semantics"在 Go 中是什么意思?

apache - 如何处理Kubernetes中Apache的资源限制

javascript - 使用默认凭据访问 Google 表格

google-cloud-platform - 如何在谷歌云存储 (Python) 中获取 blob 的 URI

google-cloud-platform - session 断开后 Google Cloud VM 文件被删除