Get list reactions from google chat with google cloud function

98 Views Asked by At

I need use the API REST for Google Chat with Google Cloud Functions and call it from POSTMAN.

I use this code https://developers.google.com/chat/api/guides/v1/reactions/list?hl=en

My problem comes in the part of the authentication. Is necessary the user authentication to get the reactions of one chat, so I don't know how authenticate when i call from Postman or ARC or other service, because the authentication screen never shows.

Page where says about the authentication types for google chat https://developers.google.com/chat/api/guides/auth?hl=en.

Searching I found that maybe I can use the domain-wide delegation to authenticate automatically to avoid the authentication screen and connect without consent.

I use this to domain-wide delegation https://support.google.com/a/answer/162106?hl=en#zippy=%2Cbefore-you-begin%2Cset-up-domain-wide-delegation-for-a-client

Another solution that I tried is getting a token authentication and send it in the call to my cloud function with Postman to authenticate automatically but it didn't work.

I use this page https://developers.google.com/oauthplayground/ to generate my authentication token

Maybe the posible solution is a mix of all of this, but I don't know how to do this.

Please somebody help me or guide me to solve this.

Thanks

EDIT.

This is my code that I use in Cloud Functions

import os.path
import flask
import functions_framework
import logging
import requests
import json
import time
import google.cloud.logging
from json import dumps
from httplib2 import Http
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
logging_client = google.cloud.logging.Client()
logging_client.setup_logging(log_level=logging.INFO)

SCOPES = 
["https://www.googleapis.com/auth/chat.messages.reactions.readonly"]

def main(event_data):
logging.info("Entro a la funcion main")

response = {
    "success": True,
    "exception": "",
    "emojiReactionSummaries": []
}

try:
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"]
    
    logging.info("Ya genero el scopes")

    '''
    Authenticates with Chat API via user credentials,
    then gets a message.
    '''

    # Authenticate with Google Workspace
    # and get user authorization.
    flow = InstalledAppFlow.from_client_secrets_file('client_secrets.json', SCOPES)
    creds = flow.run_local_server()
    logging.info("ya genero flow y creds")

    # Build a service endpoint for Chat API.
    chat = build('chat', 'v1', credentials=creds)
    logging.info("ya genero chat")

    # Use the service endpoint to call Chat API.
    result = chat.spaces().messages().get(

        # The message to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        #
        # Replace MESSAGE with a message name.
        # Obtain the message name from the response body returned
        # after creating a message asynchronously with Chat REST API.
        #name = 'spaces/AAAAseQzoLI/messages/6xjOl2HrDD0.6xjOl2HrDD0'
        name = event_data["messageName"]

    ).execute()

    # Prints details about the created reactions.
    print(result)

    logging.info("result %s" % result)

    response["emojiReactionSummaries"] = result["emojiReactionSummaries"]
except:
    response["success"] = False
    response["emojiReactionSummaries"] = []

return response
@functions_framework.http
def GetMessageReactions(request) :
 event_data = request.get_json()
 logging.info("received event_data %s" % event_data)
 objectResponse = main(event_data)
 return objectResponse
0

There are 0 best solutions below