取得會員資格詳情

本指南說明如何在 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:聊天室 name 的 ID。您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:成員的 name 中的 ID。您可以呼叫 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:聊天室 name 的 ID。您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:成員 name 中的 ID。您可以呼叫 ListMemberships() 方法來取得 ID。

Chat API 會傳回 Membership 的例項,詳細說明指定的會員資格。

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

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

如要以 Google Workspace 管理員身分呼叫這個方法,請按照下列步驟操作:

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

如需進一步瞭解相關資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。