我不确定这是否是 Cloud Pub/Sub 内部错误

I'm not sure if this is Cloud Pub/Sub internal error

最近我在测试我的 Google 云功能时开始遇到这个错误:

textPayload:  "2019/08/12 11:15:58 error publishing to the topic - rpc error: code = Unauthenticated desc = transport: compute: Received 500 `Could not fetch URI /computeMetadata/v1/instance/service-accounts/default/token?scopes=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform%2Chttps%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpubsub"

这让我很惊讶。最近我对该功能进行了一些更改,但似乎并非如此。我恢复到以前工作的旧代码,但我遇到了同样的错误。

我读了Cloud Pub/Sub docs on errors:

INTERNAL 500 This error indicates an internal server error; it should not occur. If this error occurs, please report to cloud support. The error should be transient.

我已将其报告给云支持。

不过,我不确定这是 Google 云内部错误还是我的代码引起的错误。

编辑:这是 Go 中非常简单的代码。

package testfcn

import (
    "log"
    "net/http"

    "cloud.google.com/go/pubsub"
)

func TestFcn(w http.ResponseWriter, r *http.Request) {

    ctx := r.Context()

    client, err := pubsub.NewClient(ctx, "min-fotball-staging")
    if err != nil {
        log.Print("error creating PubSub client - ", err)
        return
    }

    topic := client.Topic("TestTopic")

    result := topic.Publish(ctx, &pubsub.Message{
        Data: []byte("TestMessage"),
    })

    _, err = result.Get(ctx)
    if err != nil {
        log.Print("error publishing to the topic - ", err)
        return
    }

}

我有一个名为 TestTopic 的主题。

这不是 Cloud Pub/Sub 客户端库的内部错误。

项目的 service-<project_number>@gcf-admin-robot.iam.gserviceaccount.com 服务帐户尚未分配 Cloud Functions Service Agent 角色。

无法通过 Google Cloud Console 添加此角色,但可以使用 gcloud 命令:

gcloud projects add-iam-policy-binding <project_name> --role=roles/cloudfunctions.serviceAgent --member=serviceAccount:service-<project_number>@gcf-admin-robot.iam.gserviceaccount.com

可能有用的链接:

额外: 就我而言,此服务帐户具有 Owner 角色。这意味着 Owner 角色没有某些权限 Cloud Functions Service Agent 角色有。