Szczegółowe informacje o wspieraniu

Ten przewodnik wyjaśnia, jak używać metody get() w zasobie Membership interfejsu Google Chat API, aby uzyskać szczegółowe informacje o członkostwie w pokoju.

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

MembershipZasób określa, czy użytkownik lub aplikacja Google Chat jest zaproszony do pokoju, należy do niego czy nie.

Uwierzytelnianie za pomocą uwierzytelniania aplikacji umożliwia aplikacji w Chat uzyskiwanie informacji o członkostwie w pokojach, do których ma dostęp w Google Chat (np. w pokojach, w których jest członkiem), ale wyklucza informacje o członkostwie aplikacji w Chat, w tym o jej własnym. Uwierzytelnianie za pomocą uwierzytelniania użytkownika zwraca członkostwa w pokojach, do których uwierzytelniony użytkownik ma dostęp.

Wymagania wstępne

Node.js

  • Pokój Google Chat, którego członkiem jest uwierzytelniony użytkownik lub wywołująca aplikacja Chat. Aby uwierzytelnić się jako aplikacja do obsługi czatu, dodaj ją do pokoju.

Python

  • Pokój Google Chat, którego członkiem jest uwierzytelniony użytkownik lub wywołująca aplikacja Chat. Aby uwierzytelnić się jako aplikacja do obsługi czatu, dodaj ją do pokoju.

Java

  • Pokój Google Chat, którego członkiem jest uwierzytelniony użytkownik lub wywołująca aplikacja Chat. Aby uwierzytelnić się jako aplikacja do obsługi czatu, dodaj ją do pokoju.

Google Apps Script

  • Pokój Google Chat, którego członkiem jest uwierzytelniony użytkownik lub wywołująca aplikacja Chat. Aby uwierzytelnić się jako aplikacja do obsługi czatu, dodaj ją do pokoju.

Sprawdzanie szczegółów subskrypcji

Aby uzyskać szczegółowe informacje o członkostwie w Google Chat, w żądaniu przekaż te dane:

  • W przypadku uwierzytelniania aplikacji określ zakres autoryzacji chat.bot. W przypadku uwierzytelniania użytkownika określ zakres autoryzacji chat.memberships.readonly lub chat.memberships. Zalecamy wybór najbardziej restrykcyjnego zakresu, który nadal umożliwia działanie aplikacji.
  • Wywołaj metodę GetMembership().
  • Przekaż name subskrypcji, aby ją otrzymać. Pobierz nazwę członkostwa z zasobu członkostwa w Google Chat.

Uzyskiwanie subskrypcji z uwierzytelnianiem użytkownika

Aby uzyskać członkostwo z uwierzytelnianiem użytkownika:

Node.js

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

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

// This sample shows how to get membership 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 and MEMBER_NAME here
    name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
  };

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

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

await main();

Python

chat/client-libraries/cloud/get_membership_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.memberships.readonly"]

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

    # Initialize request argument(s)
    request = google_chat.GetMembershipRequest(
        # Replace SPACE_NAME and MEMBER_NAME here
        name = 'spaces/SPACE_NAME/members/MEMBER_NAME',
    )

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

    # Handle the response
    print(response)

get_membership_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMembershipUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMembershipRequest;
import com.google.chat.v1.Membership;

// This sample shows how to get membership with user credential.
public class GetMembershipUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetMembershipRequest.Builder request = GetMembershipRequest.newBuilder()
        // replace SPACE_NAME and MEMBERSHIP_NAME here
        .setName("spaces/SPACE_NAME/members/MEMBERSHIP_NAME");
      Membership response = chatServiceClient.getMembership(request.build());

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

Google Apps Script

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

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

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

Aby uruchomić ten przykład, zastąp te elementy:

  • SPACE_NAME: identyfikator z name pokoju. Możesz go uzyskać, wywołując metodę ListSpaces() lub z adresu URL pokoju.
  • MEMBER_NAME: identyfikator z karty uczestnika name. Identyfikator możesz uzyskać, wywołując metodę ListMemberships().

Interfejs Chat API zwraca instancję Membership zawierającą szczegółowe informacje o określonym członkostwie.

Uzyskiwanie subskrypcji za pomocą uwierzytelniania w aplikacji

Aby uzyskać subskrypcję z uwierzytelnianiem w aplikacji:

Node.js

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

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

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

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

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

await main();

Python

chat/client-libraries/cloud/get_membership_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 membership with app credential
def get_membership_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.GetMembershipRequest(
        # Replace SPACE_NAME and MEMBER_NAME here
        name = 'spaces/SPACE_NAME/members/MEMBER_NAME',
    )

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

    # Handle the response
    print(response)

get_membership_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMembershipAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMembershipRequest;
import com.google.chat.v1.Membership;

// This sample shows how to get membership with app credential.
public class GetMembershipAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetMembershipRequest.Builder request = GetMembershipRequest.newBuilder()
        // replace SPACE_NAME and MEMBERSHIP_NAME here
        .setName("spaces/SPACE_NAME/members/MEMBERSHIP_NAME");
      Membership response = chatServiceClient.getMembership(request.build());

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

Google Apps Script

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

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

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

Aby uruchomić ten przykład, zastąp te elementy:

  • SPACE_NAME: identyfikator z name pokoju. Możesz go uzyskać, wywołując metodę ListSpaces() lub z adresu URL pokoju.
  • MEMBER_NAME: identyfikator z karty uczestnika name. Identyfikator możesz uzyskać, wywołując metodę ListMemberships().

Interfejs Chat API zwraca instancję Membership zawierającą szczegółowe informacje o określonym członkostwie.

Szczegółowe informacje o subskrypcjach dla administratorów Google Workspace

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

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

  • Wywołaj metodę za pomocą uwierzytelniania użytkownika i określ zakres autoryzacji, który obsługuje wywoływanie metody z użyciem uprawnień administratora.
  • W żądaniu określ parametr zapytania useAdminAccess na true.

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