我在跟踪创建HTTP目标任务指南。当我运行下面发布的代码时,我会得到以下错误:
cloudtasks.CreateTask: rpc error: code = PermissionDenied
desc = The principal (user or service account)
lacks IAM permission "cloudtasks.tasks.create" for the resource
"projects/my_project/locations/europe-west1/queues/my_queue"
(or the resource may not exist).
我已经和gcloud auth login my@email.com
签约了。my@email.com具有由我的自定义云任务角色设置的以下权限:
我还是不明白。我还应该查些什么?
main.go
// Run `PROJECT_ID=my_project QUEUE_ID=my_queue go run main.go`
package main
import (
"context"
"fmt"
"os"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2"
)
var (
locationID = "europe-west1"
url = "example.com/callback"
message = "testing"
)
func main() {
projectID := os.Getenv("PROJECT_ID")
queueID := os.Getenv("QUEUE_ID")
task, err := createHTTPTask(projectID, locationID, queueID, url, message)
if err != nil {
fmt.Println(err)
}
fmt.Println(task)
}
// createHTTPTask creates a new task with a HTTP target then adds it to a Queue.
func createHTTPTask(projectID, locationID, queueID, url, message string) (*taskspb.Task, error) {
// Create a new Cloud Tasks client instance.
// See https://godoc.org/cloud.google.com/go/cloudtasks/apiv2
ctx := context.Background()
client, err := cloudtasks.NewClient(ctx)
if err != nil {
return nil, fmt.Errorf("NewClient: %v", err)
}
// Build the Task queue path.
queuePath := fmt.Sprintf("projects/%s/locations/%s/queues/%s", projectID, locationID, queueID)
// Build the Task payload.
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#CreateTaskRequest
req := &taskspb.CreateTaskRequest{
Parent: queuePath,
Task: &taskspb.Task{
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest
MessageType: &taskspb.Task_HttpRequest{
HttpRequest: &taskspb.HttpRequest{
HttpMethod: taskspb.HttpMethod_POST,
Url: url,
},
},
},
}
// Add a payload message if one is present.
req.Task.GetHttpRequest().Body = []byte(message)
createdTask, err := client.CreateTask(ctx, req)
if err != nil {
return nil, fmt.Errorf("cloudtasks.CreateTask: %v", err)
}
return createdTask, nil
}
启用了云任务API。
发布于 2020-02-26 22:58:16
在过去的几天里,我一直有着同样的问题,并想出了答案。用于创建API客户端和创建任务的库使用了与我预期不同的凭据。
对于使用“应用程序默认凭据”或至少让客户端自动查找凭据的用户,请查看以下页面:自动。
我创建了一个具有所有正确角色的服务帐户,并假设API客户端正在使用服务帐户。结果,我没有传入密钥文件,因此它使用的是“应用程序默认凭据”。对于我的用例,“应用程序默认凭据”引用到App默认服务帐户。当我向API客户端提供我的自定义服务帐户的密钥文件时,它起了作用。
发布于 2021-02-10 20:23:42
应用程序默认凭据(ADC)提供了一种获取用于调用Google的凭据的方法。gcloud auth应用程序-默认命令组允许您管理用于本地应用程序开发的计算机上的活动凭据。
使用以下命令获取用于ADC的新用户凭据:
gcloud auth application-default login
https://stackoverflow.com/questions/60394894
复制相似问题