Xem chi tiết về gói thành viên

Hướng dẫn này giải thích cách sử dụng phương thức get() trên tài nguyên Membership của Google Chat API để biết thông tin chi tiết về một thành viên trong không gian.

Nếu là quản trị viên Google Workspace, bạn có thể gọi phương thức get() để truy xuất thông tin chi tiết về mọi thành viên trong tổ chức Google Workspace của mình.

Tài nguyên Membership cho biết liệu người dùng là con người hay ứng dụng Google Chat có được mời, có tham gia hay không tham gia một không gian.

Xác thực bằng xác thực ứng dụng cho phép một ứng dụng Chat lấy thông tin thành viên từ những không gian mà ứng dụng đó có quyền truy cập trong Google Chat (ví dụ: những không gian mà ứng dụng đó là thành viên), nhưng không bao gồm thông tin thành viên của ứng dụng Chat, kể cả thông tin thành viên của chính ứng dụng đó. Xác thực bằng xác thực người dùng sẽ trả về thông tin thành viên của những không gian mà người dùng đã xác thực có quyền truy cập.

Điều kiện tiên quyết

Node.js

Python

Java

Apps Script

Xem thông tin chi tiết về gói thành viên

Để biết thông tin chi tiết về một thành viên trong Google Chat, hãy truyền các thông tin sau trong yêu cầu của bạn:

  • Với xác thực ứng dụng, hãy chỉ định phạm vi uỷ quyền chat.bot. Với xác thực người dùng, hãy chỉ định phạm vi uỷ quyền chat.memberships.readonly hoặc chat.memberships. Tốt nhất là bạn nên chọn phạm vi hạn chế nhất nhưng vẫn cho phép ứng dụng của bạn hoạt động.
  • Gọi phương thức GetMembership().
  • Truyền name của gói thành viên để nhận. Lấy tên thành viên từ tài nguyên thành viên của Google Chat.

Mua gói hội viên bằng cách xác thực người dùng

Sau đây là cách mua gói thành viên bằng tính năng xác thực người dùng:

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);
}

main().catch(console.error);

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));
    }
  }
}

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);
}

Để chạy mẫu này, hãy thay thế các nội dung sau:

  • SPACE_NAME: mã nhận dạng từ name của không gian. Bạn có thể lấy mã nhận dạng bằng cách gọi phương thức ListSpaces() hoặc từ URL của không gian.
  • MEMBER_NAME: mã nhận dạng từ name của thành viên. Bạn có thể lấy mã nhận dạng bằng cách gọi phương thức ListMemberships().

Chat API trả về một thực thể Membership nêu chi tiết về thành viên được chỉ định.

Mua gói hội viên bằng phương thức xác thực ứng dụng

Sau đây là cách mua gói thành viên bằng phương thức xác thực ứng dụng:

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);
}

main().catch(console.error);

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));
    }
  }
}

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);
}

Để chạy mẫu này, hãy thay thế các nội dung sau:

  • SPACE_NAME: mã nhận dạng từ name của không gian. Bạn có thể lấy mã nhận dạng bằng cách gọi phương thức ListSpaces() hoặc từ URL của không gian.
  • MEMBER_NAME: mã nhận dạng từ name của thành viên. Bạn có thể lấy mã nhận dạng bằng cách gọi phương thức ListMemberships().

Chat API trả về một thực thể Membership nêu chi tiết về thành viên được chỉ định.

Xem thông tin chi tiết về gói thành viên với tư cách là quản trị viên Google Workspace

Nếu là quản trị viên Google Workspace, bạn có thể gọi phương thức GetMembership() để truy xuất thông tin chi tiết về tư cách thành viên của bất kỳ người dùng nào trong tổ chức Google Workspace của bạn.

Để gọi phương thức này với tư cách là quản trị viên Google Workspace, hãy làm như sau:

  • Gọi phương thức bằng cách xác thực người dùng và chỉ định một phạm vi uỷ quyền hỗ trợ việc gọi phương thức bằng đặc quyền quản trị viên.
  • Trong yêu cầu, hãy chỉ định tham số truy vấn useAdminAccess thành true.

Để biết thêm thông tin và ví dụ, hãy xem bài viết Quản lý không gian trên Google Chat với tư cách là quản trị viên Google Workspace.