如何与Ruby创建并发送Google聊天消息

发布于 2025-01-31 05:48:06 字数 604 浏览 3 评论 0原文

在我的Rails应用程序中,我正在尝试使用API​​方法spaces.messages.messages.create在此文档。为此,我将不得不使用服务帐户进行身份验证。我找不到有关如何使用Ruby参考Google聊天来执行此操作的示例。似乎我将不得不使用Ruby Cloud SDK,但我找不到对Google Chat 在这里。与其使用Google Cloud SDK,不如使用Ruby HTTP客户端执行此操作吗?

我想知道如何使用服务帐户进行身份验证并使用API​​方法spaces.messages.create将消息发送到Google聊天

In my rails application, I am trying to create and send a google chat message using the api method spaces.messages.create as explained in this documentation. To do that, I will have to authenticate with a service account. I cant find examples on how to do this with ruby in reference to google chat. It seems like I will have to use the ruby cloud sdk but I cant find any reference to google chat here . Instead of using the google cloud sdk, is it possible to do this with a ruby http client?

I will like to know how to authenticate using a service account and sending a message to google chat using the api method spaces.messages.create

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2025-02-07 05:48:06

我认为这些步骤如下:

  1. 创建a 服务帐户 https://www.googleapis.com/auth/chat.bot范围。

仅支持服务帐户。您无法使用此范围来验证用户凭据。

  1. 使用
scope = 'https://www.googleapis.com/auth/androidpublisher'

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('/path/to/service_account_json_key.json'),
  scope: scope)

authorizer.fetch_access_token!

3-使用代码> 用于执行请求。

无论如何,我都强烈评论文档>用于Google Chats api 完全了解授权流程的工作原理。

更新的

样本Ruby客户端发送消息
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'google/apis/chat_v1'

scope = 'https://www.googleapis.com/auth/chat.bot'

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('./credentials.json'),
  scope: scope)


chat =  Google::Apis::ChatV1::HangoutsChatService.new
msg = Google::Apis::ChatV1::Message.new(text: 'test')

chat.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('./credentials.json'),
  scope: scope)
  
  
chat.create_space_message('spaces/XXXXXXXX', msg)

I think the steps are as follows:

  1. Create a service account in order to use the https://www.googleapis.com/auth/chat.bot scope.

Only supports service accounts. You can't authenticate user credentials using this scope.

  1. Use the Service Account Auth Flow within googleauth.
scope = 'https://www.googleapis.com/auth/androidpublisher'

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('/path/to/service_account_json_key.json'),
  scope: scope)

authorizer.fetch_access_token!

3- Use the google-apis-chat_v1 for performing the request.

In any case, I strongly review the documentation for Google Chats API, for a full understanding on how the authorization flow works.

Updated

Sample ruby client send message
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'google/apis/chat_v1'

scope = 'https://www.googleapis.com/auth/chat.bot'

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('./credentials.json'),
  scope: scope)


chat =  Google::Apis::ChatV1::HangoutsChatService.new
msg = Google::Apis::ChatV1::Message.new(text: 'test')

chat.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('./credentials.json'),
  scope: scope)
  
  
chat.create_space_message('spaces/XXXXXXXX', msg)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文