Google 来自 App Engine 的 Cloud Pubsub 身份验证错误

Google Cloud Pubsub authentication error from App Engine

我们在向 Google AppEngine 上的 Google Cloud PubSub 主题发布消息时遇到问题。使用应用程序默认凭据在本地非常有效。但是一旦部署在 Google AppEngine 上,它就会出现以下错误:

<HttpError 403 when requesting https://pubsub.googleapis.com/v1/projects/our-project-id/topics/our-topic:publish?alt=json returned "The request cannot be identified with a project. Please pass a valid API key with the request.">

我假设它将使用应用引擎的服务帐户来访问 PubSub API。这是我们用来创建凭据的代码。

credentials = GoogleCredentials.get_application_default()
if credentials.create_scoped_required():
   credentials = credentials.create_scoped(['https://www.googleapis.com/auth/pubsub'])

http = httplib2.Http()
credentials.authorize(http)
pubsub_service = build('pubsub', 'v1', http=http)

将实际消息发布到 PubSub 时抛出错误。

pubsub_service.projects().topics().publish(topic="projects/out-project-id/topics/out-topic", body = { 'messages' : [ { 'data': base64.b64encode(request.get_data()) }]}).execute()

并不是同一流程在 API 调用“BigQuery”时起作用,因此这不是一般的 Google API 问题。它似乎特定于 PubSub...

服务帐户中没有嵌入项目 ID 的情况很少见。我们修复了您的服务帐户,您现在应该可以开始了。抱歉给您带来麻烦。