如何解决Google SDM APINest 设备的 Google PubSub 订阅者问题
所以,我被卡住了。这可能会很长,因为我想建立所有(或我所做的大部分)以达到这一点:
背景:想要为 Raspberry PI 创建一个应用程序,该应用程序可以访问和操作 Nest 设备套件(门铃、恒温器等)。我目前正在笔记本电脑上的 Ubuntu VM 中执行此操作。
我做了什么:
- 按照谷歌提供的步骤创建了一个设备访问项目。
- 还创建了一个 GCP 项目(与上面的不同),以便能够设置 OAuth 和订阅设置
- Device Access 项目使用 GCP 项目中的 OAuth,即 Device Access 项目具有引用 GCP 项目 OAuth 的字段
- 完成了设备访问项目步骤,通过浏览器页面授予权限,直到我创建和使用授权代码为止,我获得了我的第一个访问令牌。我可以执行 CURL 命令来检索 Nest 设备特征,甚至可以执行 RTSP url 来访问相机流。
所以我已经完成了一半,因为我可以使用 OAuth 访问令牌与设备 (REST API) 进行交互。但我想要做的是能够对来自设备的事件做出反应,这需要 PubSub 交互性。这就是我被卡住的地方
额外步骤:我还在 GCP Credentials 中创建了一个服务帐户,因为我在文档中看到它可能是必需的,而 googles 算法需要一个 GOOGLE_APPLICATION_CREDENTIALS 环境变量到一个带有下载的服务帐户凭据的 json 文件。所以检查
- 在设置设备访问项目时,创建了一个 PubSub 主题。在 GCP 控制台中,我为 GCP 项目创建了相应的订阅,并手动将其引用到作为设备访问项目的一部分创建的主题(上述步骤)
- 文档中提到,当您第一次通过 REST API 与 OAuth 访问令牌交互时,发布订阅功能变得可用。注意:我可以从 GCP Console 的 PubSub 订阅页面确认此“查看消息”。我能够“拉”消息并看到消息出现在订阅中。我知道通过拉取消息,我暂时阻止它们出现在其他队列中,但我想检查消息是否能够填充到订阅中。
好的。所以,不,我想编写代码来访问 pubsub 订阅并围绕对这些事件消息做出反应来创建我的应用程序(或其中的一部分)。这就是我被困两天的地方。
我根据谷歌提供的方法创建了一个 python 脚本(我不介意包含 ID,因为这不是私有的)
from google.cloud import pubsub_v1
from concurrent.futures import TimeoutError
project_id = 'nestcontroller-299520'
subscription_id = 'NestEventPull'
timeout = 60.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id,subscription_id)
def callback(message):
print(f"Received a message: \n")
print(f"{message} \n")
message.ack()
future = subscriber.subscribe(subscription_path,callback=callback)
print(f"\nprojtest - listening for messages on: {subscription_path}\n")
print(f"subscription_id : {subscription_id}\n")
with subscriber:
try:
future.result(timeout=timeout)
except:
TimeoutError
future.cancel()
输出如下:
projtest - listening for messages on: projects/nestcontroller-299520/subscriptions/NestControlSub
subscription_id : NestControlSub
Background thread did not exit.
ERROR:root:Exception in callback <bound method ResumableBidiRpc._on_call_done of <google.api_core.bidi.ResumableBidiRpc object at 0x7f042ae47fa0>>: ValueError('Cannot invoke RPC: Channel closed!')
上面的代码“卡住”了 60 秒,之后“后台线程没有退出”。和错误出现
我尝试了上述的几种变体,包括另一个使用(即“PIP INSTALL”ing)google-cloud-pubsub库的python脚本,但最终我尝试了以下,我从另一个解决方案中借用了我试图找出“卡住”时发生了什么。
project_id = 'nestcontroller-299520'
subscription_id = 'NestControlSub'
topic_id = 'projects/sdm-prod/topics/enterprise-5a0f7c4e-d20d-4e56-968d-c6239936a3b0'
def callback(message,event):
logger.info(message)
event.set()
subscriber = pubsub_v1.SubscriberClient()
event = Event()
def callback_wrapper(message):
callback(message,event)
future = subscriber.subscribe('subscription/path',callback=callback_wrapper)
event.wait()
logger.exception('Got event. Shutting down.')
future.cancel()
exit(1)
这立即导致以下“无效授权:未找到帐户”错误,不断重复,直到我取消脚本...
ERROR:grpc._plugin_wrapping:AuthMetadataPluginCallback "<google.auth.transport.grpc.AuthMetadataPlugin object at 0x7f7188d17d30>" raised exception!
Traceback (most recent call last):
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/grpc/_plugin_wrapping.py",line 77,in __call__
self._metadata_plugin(
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/auth/transport/grpc.py",line 86,in __call__
callback(self._get_authorization_headers(context),None)
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/auth/transport/grpc.py",line 72,in _get_authorization_headers
self._credentials.before_request(
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/auth/credentials.py",line 133,in before_request
self.refresh(request)
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/oauth2/service_account.py",line 361,in refresh
access_token,expiry,_ = _client.jwt_grant(request,self._token_uri,assertion)
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/oauth2/_client.py",line 153,in jwt_grant
response_data = _token_endpoint_request(request,token_uri,body)
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/oauth2/_client.py",line 124,in _token_endpoint_request
_handle_error_response(response_body)
File "/home/christian/PythonProjects/google-cloud-pubsub/venv/lib/python3.8/site-packages/google/oauth2/_client.py",line 60,in _handle_error_response
raise exceptions.RefreshError(error_details,response_body)
google.auth.exceptions.RefreshError: ('invalid_grant: Invalid grant: account not found','{"error":"invalid_grant","error_description":"Invalid grant: account not found"}')
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed non-terminating stream error 503 Getting metadata from plugin failed with error: ('invalid_grant: Invalid grant: account not found',"error_description":"Invalid grant: account not found"}')
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed recoverable stream error 503 Getting metadata from plugin failed with error: ('invalid_grant: Invalid grant: account not found',"error_description":"Invalid grant: account not found"}')
INFO:google.api_core.bidi:Re-established stream
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed non-terminating stream error 503 Getting metadata from plugin failed with error: ('invalid_grant: Invalid grant: account not found',"error_description":"Invalid grant: account not found"}')
有人知道为什么 Pubsub 模型不起作用,即使我遵循了所有可能的文档变体吗?我已尝试更改凭据文件详细信息(只要我不使用服务帐户凭据,它就会给出明确定义的错误,因此这似乎不是问题。
任何帮助或提示将不胜感激。
解决方法
好的。因此,经过一夜好眠和幸运的 YouTube 视频后,我找到了遗漏的步骤。
必须为 Pub/Sub 发布者和 Pub/Sub 订阅者角色专门配置服务帐户的角色。完成此操作并重新激活服务帐户后,evnet 消息开始通过
无论如何,我希望上面的过程和结论可以帮助任何遇到同样问题的人
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。