所以,我被困住了。这可能很长,因为我想建立所有(或我所做的大部分)来达到这一点:
背景:想要为 Raspberry PI 创建一个应用程序,该应用程序可以访问和操作 Nest 设备套件(门铃、恒温器等)。我目前在笔记本电脑上的 Ubuntu VM 中执行此操作。
我做了什么:
- 按照谷歌提供的步骤创建了一个设备访问项目。
- 还创建了一个 GCP 项目(和上面不是一回事),以便能够设置 OAuth 和 Subscription 设置
- Device Access 项目使用来自 GCP 项目的 OAuth,即 Device Access 项目有一个引用 GCP 项目 OAuth 的字段
- 完成了设备访问项目步骤,通过浏览器页面授予权限,直到我创建和使用授权代码,我得到了我的第一个访问令牌。我能够执行 CURL 命令来检索 Nest 设备特征,甚至可以执行 RTSP url 来访问相机流。
所以我已经完成了一半,因为我可以通过使用 OAuth 访问令牌与设备(REST API)进行交互。但我想做的是能够对来自设备的事件做出反应,这需要 PubSub 交互性。这就是我卡住的地方
额外步骤:我还在 GCP 凭据中创建了一个服务帐户,因为我在文档中看到它可能是必需的,并且 googles 算法需要一个 GOOGLE_APPLICATION_CREDENTIALS 环境变量到一个带有服务帐户下载凭据的 json 文件。所以检查
- 在设置设备访问项目时,创建了一个 PubSub 主题。在 GCP 控制台中,我为 GCP 项目创建了相应的订阅,并手动将其引用到作为设备访问项目的一部分创建的主题(上述步骤)
- 文档中提到,当您第一次通过 REST API 与 OAuth 访问令牌进行交互时,pubsub 功能变得可用。注意:我可以从 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":"invalid_grant","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":"invalid_grant","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":"invalid_grant","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":"invalid_grant","error_description":"Invalid grant: account not found"}')
有没有人知道为什么 Pubsub 模型不起作用,即使我已经遵循了所有可能的文档变体?我尝试更改凭据文件详细信息(只要我不使用服务帐户凭据,它就会给出明确定义的错误,因此这似乎不是问题。
任何帮助或提示将不胜感激。