gcloud pubsub发布消息问题

问题描述 投票:0回答:2
I am trying to publish the message on topic but I am not able to publish the message. I am using laravelframework. My subscription is push type. 

我已使用[https://github.com/googleapis/google-cloud-php-pubsub链接的$作曲家需要google / cloud-pubsub我已关注此链接:(https://cloud.google.com/pubsub/docs/publisher#php

**use Google\Cloud\PubSub\PubSubClient;**
function publish_message($projectId, $topicName, $message)
{
    $pubsub = new PubSubClient([\[][1]
        'projectId' => $projectId,
    ]);
    $topic = $pubsub->topic($topicName);
    $topic->publish(['data' => $message]);
    print('Message published' . PHP_EOL);
}

I am getting this error (open this link : https://i.stack.imgur.com/XXHZ5.png.


  [1]: https://i.stack.imgur.com/XXHZ5.png
gcloud publish-subscribe
2个回答
0
投票

您的问题将得到更详细的解释。

原样,您显示的代码与Google发布的代码相同。

假设(!?)Google的代码有效(可能但不确定),您的代码应该有效。

由于我们知道您的代码不起作用,可能还有其他原因。

我怀疑您错过了以下一个或多个以下可能的最后步骤:

  • 创建了Google Cloud Platform项目($projectId)?
  • 已启用发布/订阅API?
  • 创建了发布/订阅主题[和> = 1个订阅者]($topicName)?
  • 已创建的(服务帐户)凭据可以发布到此主题?
  • GOOGLE_APPLICATION_CREDENTIALS设置为指向帐户的密钥吗?

您如何运行代码?

如果可能,还请print显示在图像中的ClientException

更新

我测试了Google的代码,它对我有用:

BILLING_ID=[[YOUR-BILLING]]
PROJECT_ID=[[YOUR-PROJECT]]
TOPIC_NAME=[[YOUR-TOPIC-NAME]]

gcloud projects create ${PROJECT}
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}

# Enabled Pub/Sub and create topic=subscription=${TOPIC_NAME}
gcloud services enable pubsub.googleapis.com \
--project=${PROJECT}

gcloud pubsub topics create ${TOPIC_NAME} \
--project=${PROJECT}

gcloud pubsub subscriptions create ${TOPIC_NAME} \
--topic=${TOPIC_NAME} \
--project=${PROJECT}

# Create service account ${ROBOT} and key `./${ROBOT}.json`

ROBOT=[[YOUR-ACCOUNT-NAME]]

gcloud iam service-accounts create ${ROBOT} \
--project=${PROJECT}

gcloud iam service-accounts keys create ./${ROBOT}.json \
--iam-account=${ROBOT}@${PROJECT}.iam.gserviceaccount.com \
--project=${PROJECT}

gcloud projects add-iam-policy-binding ${PROJECT} \
--member=serviceAccount:${ROBOT}@${PROJECT}.iam.gserviceaccount.com \
--role=roles/pubsub.publisher

export GOOGLE_APPLICATION_CREDENTIALS=./${ROBOT}.json

0
投票

我猜您面临的问题是因为您必须错过身份验证步骤。您是否创建了SA并下载了json文件进行身份验证?如果是这样,请再次检查文件系统中是否有此行:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-credentials.json
© www.soinside.com 2019 - 2024. All rights reserved.