我正在關注創建 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 [email protected]。[email protected] 具有由我的自定義云任務角色設置的以下權限:cloudtasks.locations.getcloudtasks.locations.listcloudtasks.queues.getcloudtasks.queues.listcloudtasks.tasks.createcloudtasks.tasks.deletecloudtasks.tasks.fullViewcloudtasks.tasks.getcloudtasks.tasks.listcloudtasks.tasks.run我不明白。我還應該檢查什么?main.go// Run `PROJECT_ID=my_project QUEUE_ID=my_queue go run main.go`package mainimport ( "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, }, }, }, }Cloud Tasks API已啟用。
2 回答

子衿沉夜
TA貢獻1828條經驗 獲得超3個贊
在過去的幾天里,我一直遇到同樣的問題,并想通了。我用來創建 API 客戶端和創建任務的庫使用的憑據與我預期的不同。
對于那些使用“應用程序默認憑據”或至少讓客戶端自動查找憑據的用戶,請查看此頁面:https ://cloud.google.com/docs/authentication/production#finding_credentials_automatically
我創建了一個具有所有正確角色的服務帳戶,并假設 API 客戶端正在使用該服務帳戶。原來我沒有傳入密鑰文件,因此它使用的是“應用程序默認憑據”。對于我的用例,“應用程序默認憑據”指的是 App Engine 默認服務帳戶。當我為 API 客戶端提供我的自定義服務帳戶的密鑰文件時,它起作用了。

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
應用程序默認憑據 (ADC) 提供了一種獲取用于調用 Google API 的憑據的方法。gcloud auth application-default 命令組允許您管理計算機上用于本地應用程序開發的活動憑據。
使用以下命令獲取用于 ADC 的新用戶憑據:
gcloud auth application-default login
- 2 回答
- 0 關注
- 146 瀏覽
添加回答
舉報
0/150
提交
取消