1

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

Hakeem Baba
  • 647
  • 1
  • 12
  • 32
  • You dont want to try to manually code service account authorizaotn. Its much easer to use the standard client. I would go with the Google cloud ruby client. – Linda Lawton - DaImTo May 23 '22 at 10:03
  • Do you know how to use the google ruby client to authenticate and make an api call to google chat. I cant find any example for that @DaImTo – Hakeem Baba May 23 '22 at 10:57

1 Answers1

1

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)
Emel
  • 2,283
  • 1
  • 7
  • 18
  • can you show me an example of how to make the api call for the method spaces.messages.create using the api gem ? – Hakeem Baba May 23 '22 at 21:09
  • Do you reviewed the steps? Do you have a service account, reviewed the documentation and have an authorized ruby app? – Emel May 23 '22 at 22:12
  • I have reviewed the steps, have a service account and have an app with the api's enabled. I just cant find an example oh how to make google chat api calls in ruby. – Hakeem Baba May 24 '22 at 08:27
  • @HakeemBaba Included an example, please up-vote or accept the answer if you consider it helpful – Emel May 24 '22 at 15:16
  • Can you please help with this https://stackoverflow.com/questions/75486527/how-to-create-and-send-a-threaded-message-in-google-chat-with-ruby ? – Hakeem Baba Feb 20 '23 at 09:17