取得會員資格詳情

本指南說明如何使用 Google Chat API 的 Membership 資源中的 get() 方法,取得聊天室成員的詳細資料。

如果您是 Google Workspace 管理員,可以呼叫 get() 方法,擷取 Google Workspace 機構中任何成員的詳細資料。

Membership 資源代表使用者或 Google Chat 應用程式是否受邀加入、參與或缺席聊天室。

使用應用程式驗證進行驗證後,Chat 應用程式就能從 Google Chat 中有權存取的聊天室 (例如應用程式是成員的聊天室) 取得成員資格,但會排除 Chat 應用程式的成員資格 (包括應用程式本身的成員資格)。使用使用者驗證進行驗證,會傳回已驗證使用者可存取空間的成員資格。

必要條件

Node.js

Python

Java

Apps Script

查看會員方案詳細資料

如要取得 Google Chat 會員的詳細資料,請在要求中傳遞下列項目:

  • 使用應用程式驗證,指定chat.bot授權範圍。透過使用者驗證,指定 chat.memberships.readonlychat.memberships 授權範圍。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。
  • 呼叫 GetMembership() 方法。
  • 傳遞要取得的會員 name。從 Google Chat 的成員資源取得成員名稱。

透過使用者驗證取得會員資格

如要透過使用者驗證取得會員資格,請按照下列步驟操作:

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

如要執行這個範例,請替換下列項目:

  • SPACE_NAME:來自聊天室的 ID name。 您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:成員的 ID,來自 name。 您可以呼叫 ListMemberships() 方法來取得 ID。

Chat API 會傳回 Membership 執行個體,詳細說明指定成員。

透過應用程式驗證取得會員資格

如要透過應用程式驗證取得會員資格,請按照下列步驟操作:

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

如要執行這個範例,請替換下列項目:

  • SPACE_NAME:來自聊天室的 ID name。 您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:成員的 ID,來自 name。 您可以呼叫 ListMemberships() 方法來取得 ID。

Chat API 會傳回 Membership 執行個體,詳細說明指定成員。

以 Google Workspace 管理員身分查看會員資格詳細資料

如果您是 Google Workspace 管理員,可以呼叫 GetMembership() 方法,擷取 Google Workspace 機構中任何使用者的成員資格詳細資料。

如要以 Google Workspace 管理員身分呼叫這個方法,請執行下列操作:

  • 使用使用者驗證呼叫方法,並指定支援使用管理員權限呼叫方法的授權範圍
  • 在要求中,將查詢參數 useAdminAccess 指定為 true

如需更多資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。