通过 SDK 在 GCP 中创建服务帐户时出错

Error while creating service account in GCP via SDK

我一直在使用 Google 的 SDK 来执行 API 调用,例如创建服务帐户、创建服务帐户密钥、获取存储桶等。最近我无法由于以下错误无法创建服务帐户。

要求:

curl --location --request POST 'https://iam.googleapis.com/v1/projects/myproject/serviceAccounts' \
--header 'Authorization: Bearer <<token>>' \
--header 'Accept-Encoding: gzip' \
--header 'User-Agent: my test Google-API-Java-Client'
   c.g.a.c.g.j.GoogleJsonResponseException: 404 Not Found
   {
     "code" : 404,
     "errors" : [ {
     "domain" : "global",
     "message" : "Not found; Gaia id not found for email xxxxx@myproject.iam.gserviceaccount.com",
    "reason" : "notFound"
  } ],
  "message" : "Not found; Gaia id not found for email xxxxx@myproject.iam.gserviceaccount.com",
  "status" : "NOT_FOUND"
}

为什么我在创建服务帐户时需要 Gaia ID?我在哪里可以找到这个ID?感谢您的帮助!!

看来你的请求需要有body

请求正文包含具有以下结构的数据:

{
  "accountId": string,
  "serviceAccount": {
    object (ServiceAccount)
  }
}

而且您的命令中缺少它。

您可以在此documentation中获得更多信息。

在你的情况下应该是这样的:

curl --request POST \
  'https://iam.googleapis.com/v1/projects/my-little-project/serviceAccounts?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"accountId":"sa-test","serviceAccount":{"description":"sa-description-test","displayName":"sa-display-testonly"}}' \
  --compressed

你可以在下面直接测试这个APIlink

此外,我在 中发现了一个类似的错误,根据此答案,如果未启用 APIs,则可能会生成此错误。

您是否删除过服务帐户 'xxxxx@myproject.iam.gserviceaccount.com',它应该是 IAM API 的默认服务帐户,您可以 recover it within the 30days after the deletion。或者只是禁用它并 re-enable 它,将为您重新创建默认服务帐户。让我知道它是否解决了问题。