Google 本地 pubsub 模拟器不允许我创建主题

Google local pubsub emulator not letting me create topics

我完全按照此处提供的步骤进行操作 https://cloud.google.com/pubsub/docs/emulator,但在使用模拟器步骤的第 4 步中出现以下错误。

因此,当我尝试使用 Google python 库创建主题时,如 link 中所述,我得到:

File "string", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 User not authorized to perform this action.

status = StatusCode.PERMISSION_DENIED
    details = "User not authorized to perform this action."
    debug_error_string = "{"created":"@1614133564.919217000","description":"Error received from peer ipv6:[2607:f8b0:4005:807::200a]:443","file":"src/core/lib/surface/call.cc","file_line":1068,"grpc_message":"User not authorized to perform this action.","grpc_status":7}"

如果我尝试使用本地 pubsub 模拟器,为什么会出现这样的错误?

看起来这些命令不是运行针对模拟器的,而是针对实际服务的。说明向 运行 说明“在您设置模拟器环境变量的机器上”的步骤。您是否按照 doc it links to 中的步骤设置了环境变量?

您首先需要 运行 的命令顺序是:

$ gcloud beta emulators pubsub start --project=my-test-project

$ (gcloud beta emulators pubsub env-init)

这将启动模拟器,然后设置环境变量PUBSUB_EMULATOR_HOST

这应该可以解决:

export PUBSUB_EMULATOR_HOST=localhost:8085

为什么

google tutorial (gcloud beta emulators pubsub env-init) 中的命令出于某种原因没有设置环境变量。因此,您的 pubsub api 调用服务帐户而不是本地模拟器,这会导致权限错误。

手动设置并使用 echo $PUBSUB_EMULATOR_HOST

验证是否已设置