0
const { google } = require('googleapis')
const privatekey = require('./a.json')
const scopes = ['https://www.googleapis.com/auth/chat.bot'];

const a = async () => {
    try {
        const jwtClient = new google.auth.JWT(
            privatekey.client_email,
            null,
            privatekey.private_key,
            scopes,
            'adminEmail@org.com'
        );
        await jwtClient.authorize();
        const chat = google.chat({ version: 'v1', auth: jwtClient });
        const res = await chat.spaces.messages.get({name:'spaces/XXX/messages/XX.XX'})
        console.log(res)
    }
    catch(e) {
        console.log(e)
    }
}
a()

Error: Request contains an invalid argument

I am unable to find the invalid argument

Thanks in advance

  • If you use `'https://www.googleapis.com/auth/chat` for `scopes` doe sit solver the issue? Is it the building of the JWT client or `chat.spaces.messages.get` that gives you the error? – ziganotschka Jul 22 '20 at 05:25
  • Yeah I tried it It says 'Insufficient Permission' I have also added ```https://www.googleapis.com/auth/chat``` in Domain-wide delegation. I am creating a bot which tries to get messages from the room in which it is already installed – Kanishk Srivastava Jul 22 '20 at 05:48
  • "Insufficient permisions" means that you did not authorize the necessary scope (`https://www.googleapis.com/auth/chat` instead of `https://www.googleapis.com/auth/chat.bot`) for domain-wide delgation. Don't forget to delete your token file each time you change the scopes. `https://www.googleapis.com/auth/chat.bot` should only be used for service accounts without impersonisation. See [here](https://issuetracker.google.com/109758946). – ziganotschka Jul 22 '20 at 09:58
  • Mind that for certain requests a [service account is required](https://developers.google.com/hangouts/chat/how-tos/service-accounts#when_is_a_service_account_needed). THis means service account without impersonation. – ziganotschka Jul 22 '20 at 09:58
  • @ziganotschka yeah thanks it is working for get messages, just needed to create a new API Key, but now is it possible to delete messages from the chat bot, there are APIs but the chatbot is only able to delete message sent by it, and not by other users. – Kanishk Srivastava Jul 27 '20 at 07:29
  • No, the chat bot cannot delete messages of other users. – ziganotschka Jul 27 '20 at 07:48
  • I will post my comment as an answer in case other users who encounter this problem find it useful. – ziganotschka Aug 04 '20 at 07:26

1 Answers1

0

Many Hangouts API request require the usage of a service account

  • You can consult in the documentation which type of requests are affected
  • For the requests requiring the usage of a service account - it is meant that the service account acts on its own behalf
  • Impersonation means that the service account acts on behalf of another user
  • Thus, impersonation is not allowed for requests that need to be carried out by a service account
  • Also mind that https://www.googleapis.com/auth/chat.bot is the scope to be used by the service account without domain-wide delegation
  • Users or impersonated service accounts need to use the scope https://www.googleapis.com/auth/chat instead - see also here
  • Last but not least, chat bots are not allowed to delete messages of other users
ziganotschka
  • 25,866
  • 2
  • 16
  • 33