5

I followed incoming bot python to send a message with webhook and I want to delete it with Method:delete.

But I get error 401 and following is the error message.

{'error': {'code': 401,
           'message': 'Request is missing required authentication credential. '
                      'Expected OAuth 2 access token, login cookie or other '
                      'valid authentication credential. See '
                      'https://developers.google.com/identity/sign-in/web/devconsole-project.',
           'status': 'UNAUTHENTICATED'}}

Webhook seems to use key and token as authorization. But I cannot delete the message with the same url (with the target "data-id").

Chao
  • 179
  • 1
  • 8

2 Answers2

0

You must have downloaded the credential file. If not then follow this.

Once you download then follow the below steps to generate Auth token.

  1. Set the environment variable

    export GOOGLE_APPLICATION_CREDENTIALS=

  2. Run this command on your terminal.

    gcloud auth application-default print-access-token

  3. Use generated token in your Postman -> Authorization -> Type OAuth 2.0 -> Access token OR In your headers as

    headers = { 'Content-Type': "application/json", 'Authorization': "Bearer ", ... }

Yog
  • 21
  • 3
0

It is not possible to delete the messages sent via Webhook in google hangouts chat, or at least that feature is not yet built.

If you are posting a message into a room or sending someone a DM via the project's Appscript code itself (without using Webhook, i.e via spaces.messages.create), then it is possible to delete it using this method spaces.messages.delete (check here)

If you try using the above method (spaces.messages.delete) to delete the message posted using Webhook, then you will get the following error

{
    "error": {
        "code": 403,
        "message": "Request had insufficient authentication scopes.",
        "status": "PERMISSION_DENIED"
    }
}

This is because the message you are trying to delete was posted by a webhook and not via the appscript code base or using spaces.messages.create and thus the access token generated does not have the privilege to delete the message since it belongs to the appscript project and not the webhook bot.

Hope that clears your question!

Securiosity
  • 37
  • 11
  • I don't think the OP mentions anything about Apps Script. I guess there's no way to delete webhook messages for now. – idontknow Nov 20 '20 at 01:28
  • It mentions using the python code to achieve the same functionality which I did via AppScript. Makes no difference, really. – Securiosity Nov 27 '20 at 13:27