Outbound REST message to Google Chat API returns an error

Lakpriya Premas
Kilo Contributor

Hi All,

I'm new to ServiceNow and I do not possess any knowledge of coding.
I want to send an outbound REST message to Google Chat whenever a P1, P2, and P3 case is created.

I have followed the document https://docs.servicenow.com/bundle/rome-servicenow-platform/page/administer/integrationhub-store-spokes/task/setup-g-hangouts.html and created below,

From ServiceNow end,
Connection Alias, Credentials, Connection, System OAuth Application Registry. Enabled all the possible scopes under Application Registry/ OAuth Entity Scopes for testing purposes. 

From Google End,
A New Project, Enabled and configured Google Chat API, Created a Service Account. Enabled all the possible scopes under App Registration.

Test Case,
Created an Action on Flow Designer with  a REST API Get method to call https://chat.googleapis.com/v1/spaces for testing purposes to check the status of the connection. 

Result,
Problem 01 - OAuth token releases but is only available for exact 60 minutes.

Problem 02 - REST Action Return an Error,

"error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
        "domain": "googleapis.com",
        "metadata": {
          "service": "chat.googleapis.com",
          "method": "google.chat.v1.ChatService.ListSpaces"

 I have tried all the possible solutions related to the error and still no luck.

Many thanks if someone can help. 

1 ACCEPTED SOLUTION

Maximilian Was1
Tera Expert

Hi,

there is an issue in Google Chat API. This is not related to ServiceNow.

As you can read here: Google Chat REST API Docs you should define the OAUTH2 scope to "https://www.googleapis.com/auth/chat.bot" but if you take a look at the oauth2 scope definitions for google OAuth 2.0 Scopes for Google APIs there is no scope for chat. 

Solution

Use a Bot with a incomming webhook insted. 

Create a Webhook in Google Chat and save the generated URL which looks like: https://chat.googleapis.com/v1/spaces/AAAAXXXXXX/messages?key=XXXXXXX&token=XXXXXXX

You can use a POST request to send a Card like this:

find_real_file.png

Create a Rest Message in ServiceNow

find_real_file.png

find_real_file.png

Add a Body:

{
    "cards": [
        {
            "sections": [
                {
                    "widgets": [
                        {
                            "keyValue": {
                                "topLabel": "${top_label}",
                                "content": "${content}",
                                "contentMultiline": "true",
                                "bottomLabel": "${bottom_label}",
                                "onClick": {
                                     "openLink": {
                                        "url": "${url}"
                                     }
                                 },
                                "icon": "${icon}",
                                "button": {
                                    "textButton": {
                                       "text": "${button_text}",
                                       "onClick": {
                                           "openLink": {
                                                "url": "${button_url}"
                                            }
                                        }
                                      }
                                 }
                             }
                        }
                    ]
                }
            ]
        }
    ]
}

find_real_file.png

Auto-generate variables and set some test values. 

find_real_file.png

Now you can save and test it.

Create a FLOW Action

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

Create a Flow and use the Action

find_real_file.png

find_real_file.png

 

View solution in original post

7 REPLIES 7

I think the Google Documentation is inconsistent.

If you take a browser, and go to the URL for the scope, you get:

 

https://www.googleapis.com/auth/chat.bot

You are receiving this error either because your input OAuth2 scope name is invalid or it refers to a newer scope that is outside the domain of this legacy API.

This API was built at a time when the scope name format was not yet standardized. This is no longer the case and all valid scope names (both old and new) are catalogued at https://developers.google.com/identity/protocols/oauth2/scopes. Use that webpage to lookup (manually) the scope name associated with the API you are trying to call and use it to craft your OAuth2 request.

 

Now, If I go to the page referenced above, I don't see the Chat/Bot scope available.

Further, If I go to the documentation for the API, and up one level, I see as you mention that a Service Account is needed.

find_real_file.png

 

Not sure what Google is up to, but this one is beyond me…

 

If correct or helpful, please indicate so!

Hi Mattias,

Thank you very much for looking into this matter. Yes, available docs are so inconsistent and conflicts with Google Hangouts implementation as well. All the available documentation from ServiceNow are for Google Hangouts, which is an obsolete application with no further support from Google.

I have noticed that I'm getting above error when trying to reach Google Chat API on https://chat.googleapis.com. Despite whether I'm using an OAuth 2.0 Client ID or Service Account to configure the Token access. This Google Chat API is not listed on Google Playground as well. When added manually and tested same error return for authentication. 

Then I have changed the API call to https://www.googleapis.com for the same configuration and it connects and releases a token without error. 

But still my objective to create a chat is not complete. Let's see.

Thanks again for the support.

Maximilian Was1
Tera Expert

Hi,

there is an issue in Google Chat API. This is not related to ServiceNow.

As you can read here: Google Chat REST API Docs you should define the OAUTH2 scope to "https://www.googleapis.com/auth/chat.bot" but if you take a look at the oauth2 scope definitions for google OAuth 2.0 Scopes for Google APIs there is no scope for chat. 

Solution

Use a Bot with a incomming webhook insted. 

Create a Webhook in Google Chat and save the generated URL which looks like: https://chat.googleapis.com/v1/spaces/AAAAXXXXXX/messages?key=XXXXXXX&token=XXXXXXX

You can use a POST request to send a Card like this:

find_real_file.png

Create a Rest Message in ServiceNow

find_real_file.png

find_real_file.png

Add a Body:

{
    "cards": [
        {
            "sections": [
                {
                    "widgets": [
                        {
                            "keyValue": {
                                "topLabel": "${top_label}",
                                "content": "${content}",
                                "contentMultiline": "true",
                                "bottomLabel": "${bottom_label}",
                                "onClick": {
                                     "openLink": {
                                        "url": "${url}"
                                     }
                                 },
                                "icon": "${icon}",
                                "button": {
                                    "textButton": {
                                       "text": "${button_text}",
                                       "onClick": {
                                           "openLink": {
                                                "url": "${button_url}"
                                            }
                                        }
                                      }
                                 }
                             }
                        }
                    ]
                }
            ]
        }
    ]
}

find_real_file.png

Auto-generate variables and set some test values. 

find_real_file.png

Now you can save and test it.

Create a FLOW Action

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

Create a Flow and use the Action

find_real_file.png

find_real_file.png