问题描述
我正在尝试 GCP Python SDK 的 cloudiot_pubsub_example_server.py 示例代码。概述有两个代码客户端和服务器。客户端发布到 GCP PUB/SUB 中的主题并更新或发布随机温度。服务器订阅此主题并接收温度。服务器也会发布到客户端的配置主题,并在某个温度升高或降低时打开或关闭风扇。
当我使用提供的所有凭据运行这两个代码时,客户端正在发布温度,服务器已订阅并获取临时数据。但是当服务器发布到配置主题并将 FAN 发送到 ON 或 OFF 时,我得到:
执行 ModifyCloudToDeviceConfig 时出错:
为了执行服务器代码,我使用了以下命令:
python3 cloudiot_pubsub_example_server.py --project_id=project-aura-249003 --pubsub_subscription=temp
为了执行客户端代码,我使用了以下命令:
python3 cloudiot_pubsub_example_mqtt_device.py --project_id=project-aura-249003 --registry_id=Linux_PC --device_id=linux_pc --private_key_file=rsa_private.pem --algorithm=RS256 --ca_certs=roots.pem --cloud_region=asia-east1
您可以在此处找到执行代码的所有文件:https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/iot/api-client/mqtt_example
我也提供了roots.pem文件 请帮我解决这个问题,403 错误与某些身份验证问题有关。
我在这里遗漏了什么?
解决方法
请确保您之前已定义环境变量 GOOGLE_APPLICATION_CREDENTIALS='your_service_account_credentials.json'
或在运行 --service_account_json="your_service_account_credentials.json"
时包含选项 cloudiot_pubsub_example_server.py
。
您的 python 命令应该如下所示:
python3 cloudiot_pubsub_example_server.py --project_id=project-aura-249003 --pubsub_subscription=temp --service_account_json="your_service_account_credentials.json"
还要仔细检查您的关联服务帐户是否具有根据 end to end IoT tutorial 的 EDITOR 角色。
我确实遵循了 end to end IoT tutorial 并且没有遇到任何问题。一切都在云壳中完成。在学习本教程之前,我创建了以下内容:
- 发布/订阅主题
- 发布/订阅订阅
- 设备注册
运行 cloudiot_pubsub_example_mqtt_device.py:
python3 cloudiot_pubsub_example_mqtt_device.py --project_id=my-project-id --registry_id=my-registry --device_id=device-****** --private_key_file=rsa_private.pem --algorithm=RS256 --ca_certs=roots.pem
cloudiot_pubsub_example_mqtt_device.py 的输出:
运行 cloudiot_pubsub_example_server.py:
python3 cloudiot_pubsub_example_server.py --project_id=my-project-id --pubsub_subscription=my-sub-****** --service_account_json="my_credentials.json"
cloudiot_pubsub_example_server.py 的输出: