Google PubSub订阅服务器出现Google SDM API(Nest设备)问题



所以,我被卡住了。这可能很长,因为我想建立所有(或我所做的大部分(来达到这一点:

背景:想为Raspberry PI创建一个应用程序,该应用程序可以访问Nest设备套件(Doorbell、恒温器等(并对其进行操作。我目前正在笔记本电脑上的Ubuntu虚拟机中进行此操作。

我所做的:

  1. 按照谷歌提供的步骤创建了一个设备访问项目
  2. 还创建了一个GCP项目(与上面的不同(,以便能够设置OAuth和Subscription设置
  3. 设备访问项目使用来自GCP项目的OAuth,即设备访问项目有一个引用GCP项目OAuth的字段
  4. C完成了设备访问项目的步骤,通过浏览器页面授予权限,直到我创建并使用授权代码,我获得了第一个访问令牌。我能够执行CURL命令来检索Nest设备特征,甚至可以执行RTSP url来访问相机流

所以我已经完成了一半,因为我可以通过使用OAuth访问令牌与设备(RESTneneneba API(进行交互。但我想做的是能够对来自设备的事件做出反应,这需要PubSub的交互性。这就是我被卡住的地方

额外步骤:我还在GCP Credentials中创建了一个服务帐户,因为我在文档中看到它可能是必需的,而谷歌算法需要一个GOOGLE_APPLICATION_CREDENTICS环境变量到一个json文件中,该文件包含服务帐户的下载凭据。所以检查

  1. 在设置设备访问项目时,创建了一个PubSub主题。在GCP控制台中,我为GCP项目创建了一个相应的Subscription,并将其手动引用到作为设备访问项目一部分创建的主题中(以上步骤(
  2. 文档中提到,第一次通过RESTneneneba API与OAuth访问令牌交互时,pubsub功能就可用了。NB:我可以确认这一点;查看消息";来自GCP控制台中的PubSub Subscription页面。我能够";"拉";消息,并查看订阅中显示的消息。我知道,通过拉取消息,我暂时阻止它们出现在其他队列中,但我想检查这些消息是否能够在订阅中填充

好的。所以,不,我想写代码来访问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(谷歌云pubsub库的python脚本,但最终我尝试了以下内容,我从我发现的另一个解决方案中借用了这些内容,试图找出在"PIP安装"时发生了什么;"卡住";。

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模型不起作用吗,尽管我已经遵循了所有可能的文档变体?我已经尝试更改凭据文件的详细信息(每当我不使用服务帐户凭据时,它都会给出明确定义的错误,所以这似乎不是问题所在。

如有任何帮助或提示,我们将不胜感激。

好的。所以,在睡了一个好觉,在YouTube上看了一段幸运的视频后,我发现了缺失的一步。

服务帐户的角色必须专门针对发布者/子发布者和发布者/订阅者角色进行配置。一旦完成并重新激活服务帐户,evnet消息就开始通过

无论如何,我希望上面的过程和结论能帮助任何陷入同样问题的人

最新更新