Wyświetlanie informacji o pokoju

Ten przewodnik wyjaśnia, jak używać metody get() w zasobie Space interfejsu Google Chat API, aby wyświetlać szczegóły pokoju, takie jak nazwa wyświetlana, opis i wskazówki.

Jeśli jesteś administratorem Google Workspace, możesz wywołać metodę get(), aby pobrać szczegóły dowolnego miejsca w organizacji Google Workspace.

SpaceZasób reprezentuje miejsce, w którym użytkownicy i aplikacje do obsługi czatu mogą wysyłać wiadomości, udostępniać pliki i współpracować. Istnieje kilka rodzajów pokoi:

  • Wiadomości na czacie to rozmowy między 2 użytkownikami lub między użytkownikiem a aplikacją do obsługi czatu.
  • Czaty grupowe to rozmowy między co najmniej 3 użytkownikami i aplikacjami do czatowania.
  • Pokoje z nazwami to trwałe miejsca, w których użytkownicy mogą wysyłać wiadomości, udostępniać pliki i współpracować.

Uwierzytelnianie za pomocą uwierzytelniania aplikacji umożliwia aplikacji do obsługi czatu uzyskanie szczegółowych informacji o pokoju, w którym jest ona członkiem. Uwierzytelnianie za pomocą uwierzytelniania użytkownika umożliwia uzyskanie dostępu do pokoi, do których uwierzytelniony użytkownik ma dostęp jako członek pokoju lub administrator Google Workspace.

Wymagania wstępne

Node.js

Python

Java

Google Apps Script

Uzyskiwanie dostępu do pokoju

Aby utworzyć pokój w Google Chat, w żądaniu przekaż te informacje:

Wyświetlanie szczegółów pokoju jako użytkownik

Aby uzyskać szczegółowe informacje o przestrzeni z uwierzytelnianiem użytkownika:

Node.js

chat/client-libraries/cloud/get-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.spaces.readonly'];

// This sample shows how to get space with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    name: 'spaces/SPACE_NAME',
  };

  // Make the request
  const response = await chatClient.getSpace(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/get_space_user_cred.py
from authentication_utils import create_client_with_user_credentials
from google.apps import chat_v1 as google_chat

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

# This sample shows how to get space with user credential
def get_space_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.GetSpaceRequest(
        # Replace SPACE_NAME here
        name = "spaces/SPACE_NAME",
    )

    # Make the request
    response = client.get_space(request)

    # Handle the response
    print(response)

get_space_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetSpaceUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetSpaceRequest;
import com.google.chat.v1.Space;

// This sample shows how to get space with user credential.
public class GetSpaceUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.spaces.readonly";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetSpaceRequest.Builder request = GetSpaceRequest.newBuilder()
        // Replace SPACE_NAME here
        .setName("spaces/SPACE_NAME");
      Space response = chatServiceClient.getSpace(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Google Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to get space with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function getSpaceUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const name = 'spaces/SPACE_NAME';

  // Make the request
  const response = Chat.Spaces.get(name);

  // Handle the response
  console.log(response);
}

Aby uruchomić ten przykład, zastąp SPACE_NAME identyfikatorem z pola name przestrzeni. Możesz go uzyskać, wywołując metodę ListSpaces() lub z adresu URL pokoju.

Interfejs Chat API zwraca instancję Space, która zawiera szczegółowe informacje o określonym pokoju.

Uzyskiwanie szczegółowych informacji o przestrzeni jako administrator Google Workspace

Jeśli jesteś administratorem Google Workspace, możesz wywołać metodę GetSpace w celu pobrania szczegółowych informacji o dowolnym pokoju w organizacji Google Workspace.

Aby wywołać tę metodę jako administrator Google Workspace, wykonaj te czynności:

Więcej informacji i przykłady znajdziesz w artykule Zarządzanie pokojami w Google Chat jako administrator Google Workspace.

Pobieranie szczegółów pokoju jako aplikacja Google Chat

Aby uzyskać szczegółowe informacje o przestrzeni za pomocą uwierzytelniania aplikacji:

Node.js

chat/client-libraries/cloud/get-space-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to get space with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    name: 'spaces/SPACE_NAME',
  };

  // Make the request
  const response = await chatClient.getSpace(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/get_space_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to get space with app credential
def get_space_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.GetSpaceRequest(
        # Replace SPACE_NAME here
        name = "spaces/SPACE_NAME",
    )

    # Make the request
    response = client.get_space(request)

    # Handle the response
    print(response)

get_space_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetSpaceAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetSpaceRequest;
import com.google.chat.v1.Space;

// This sample shows how to get space with app credential.
public class GetSpaceAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetSpaceRequest.Builder request = GetSpaceRequest.newBuilder()
        // Replace SPACE_NAME here
        .setName("spaces/SPACE_NAME");
      Space response = chatServiceClient.getSpace(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Google Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to get space with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function getSpaceAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const name = 'spaces/SPACE_NAME';
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.get(name, parameters, getHeaderWithAppCredentials());

  // Handle the response
  console.log(response);
}

Aby uruchomić ten przykład, zastąp SPACE_NAME identyfikatorem z pola name przestrzeni. Możesz go uzyskać, wywołując metodę ListSpaces() lub z adresu URL pokoju.

Interfejs Chat API zwraca instancję Space, która zawiera szczegółowe informacje o określonym pokoju.

Ograniczenia i kwestie do rozważenia