स्पेस के सदस्यों की सूची बनाना

इस गाइड में बताया गया है कि Google Chat API के Membership संसाधन पर list() तरीके का इस्तेमाल कैसे किया जाता है. इससे किसी स्पेस में मौजूद सदस्यों को पेज के हिसाब से व्यवस्थित किया जा सकता है. साथ ही, उन्हें स्पेस में सदस्यता के हिसाब से फ़िल्टर किया जा सकता है.

  • ऐप्लिकेशन की पुष्टि करने की सुविधा के साथ सदस्यताएं दिखाने पर, उन स्पेस की सदस्यताएं दिखती हैं जिनका ऐक्सेस Chat ऐप्लिकेशन के पास है. हालांकि, इसमें Chat ऐप्लिकेशन की सदस्यताएं शामिल नहीं होती हैं. इसमें खुद की सदस्यता भी शामिल नहीं होती है.
  • उपयोगकर्ता की पुष्टि करके सदस्यताएं दिखाने की सुविधा, उन स्पेस में मौजूद सदस्यताओं को दिखाती है जिन्हें पुष्टि किए गए उपयोगकर्ता ने ऐक्सेस किया है.
  • Google Workspace एडमिन के तौर पर, उपयोगकर्ता की पुष्टि करने के लिए एडमिन के विशेषाधिकार का इस्तेमाल करके, सदस्यताएं देखने की सुविधा मिलती है. इससे, आपको अपने Google Workspace संगठन के सभी स्पेस में मौजूद सदस्यताएं दिखती हैं.

Membership संसाधन से पता चलता है कि किसी व्यक्ति या Google Chat ऐप्लिकेशन को स्पेस में शामिल होने का न्योता मिला है या नहीं. इससे यह भी पता चलता है कि वह स्पेस में शामिल है या नहीं.

ज़रूरी शर्तें

Node.js

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Python

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Java

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Apps Script

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

उपयोगकर्ता की पुष्टि करने की सुविधा के साथ, किसी स्पेस में मौजूद सदस्यों की सूची बनाना

किसी ऐसे स्पेस में उपयोगकर्ताओं, Google Groups, और Chat ऐप्लिकेशन को लिस्ट करने के लिए जिसमें पुष्टि किए गए उपयोगकर्ता के पास ऐक्सेस है, अपने अनुरोध में यह जानकारी शामिल करें:

  • उपयोगकर्ता की पुष्टि करके, chat.memberships.readonly या chat.memberships अनुमति का दायरा तय करें.
  • ListMemberships() तरीके को कॉल करें.
  • Google ग्रुप की सूची बनाने के लिए, क्वेरी पैरामीटर showGroups को true पर सेट करें.

यहां दिए गए उदाहरण में, Google ग्रुप, व्यक्ति, और ऐप्लिकेशन के उन सदस्यों की सूची दी गई है जो पुष्टि किए गए उपयोगकर्ता को दिखते हैं.

Node.js

chat/client-libraries/cloud/list-memberships-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 list memberships 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
    parent: 'spaces/SPACE_NAME',
    // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
    // ROLE_MANAGER)
    filter: 'member.type = "HUMAN"'
  };

  // Make the request
  const pageResult = chatClient.listMembershipsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

main().catch(console.error);

Python

chat/client-libraries/cloud/list_memberships_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 list memberships with user credential
def list_memberships_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.ListMembershipsRequest(
        # Replace SPACE_NAME here
        parent = 'spaces/SPACE_NAME',
        # Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
        # ROLE_MANAGER)
        filter = 'member.type = "HUMAN"',
        # Number of results that will be returned at once
        page_size = 100
    )

    # Make the request
    page_result = client.list_memberships(request)

    # Handle the response. Iterating over page_result will yield results and
    # resolve additional pages automatically.
    for response in page_result:
        print(response)

list_memberships_user_cred()

Java

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

// This sample shows how to list memberships with user credential.
public class ListMembershipsUserCred {

  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))) {
      ListMembershipsRequest.Builder request = ListMembershipsRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        // Filter membership by type (HUMAN or BOT) or role
        // (ROLE_MEMBER or ROLE_MANAGER).
        .setFilter("member.type = \"HUMAN\"")
        // Number of results that will be returned at once.
        .setPageSize(10);

      // Iterating over results and resolve additional pages automatically.
      for (Membership response :
          chatServiceClient.listMemberships(request.build()).iterateAll()) {
        System.out.println(JsonFormat.printer().print(response));
      }
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to list memberships 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 listMembershipsUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const parent = 'spaces/SPACE_NAME';
  // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
  // ROLE_MANAGER)
  const filter = 'member.type = "HUMAN"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.Members.list(parent, {
      filter: filter,
      pageSize: 10,
      pageToken: pageToken
    });
    // Handle response pages
    if (responsePage.memberships) {
      responsePage.memberships.forEach((membership) => console.log(membership));
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

इस सैंपल को चलाने के लिए, SPACE_NAME को स्पेस के name फ़ील्ड से मिले आईडी से बदलें. आईडी पाने के लिए, ListSpaces() तरीके का इस्तेमाल करें या स्पेस के यूआरएल से आईडी पाएं.

Google Chat API, चुने गए स्पेस में शामिल Google ग्रुप, लोगों, और ऐप्लिकेशन के सदस्यों की सूची दिखाता है.

ऐप्लिकेशन की पुष्टि करने की सुविधा वाले स्पेस में मौजूद सदस्यों की सूची

किसी ऐसे स्पेस में उपयोगकर्ताओं और Chat ऐप्लिकेशन को लिस्ट करने के लिए जिसका ऐक्सेस पुष्टि किए गए ऐप्लिकेशन के पास है, अपने अनुरोध में यह जानकारी शामिल करें:

  • ऐप्लिकेशन की पुष्टि करके, chat.bot के लिए अनुमति का दायरा तय करें.
  • ListMemberships() तरीके को कॉल करें.
  • Google ग्रुप की सूची बनाने के लिए, क्वेरी पैरामीटर showGroups को true पर सेट करें.

यहां दिए गए उदाहरण में, स्पेस के उन सदस्यों (स्पेस मैनेजर नहीं) की सूची दी गई है जो Chat ऐप्लिकेशन को दिखते हैं:

Node.js

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    parent: 'spaces/SPACE_NAME',
    // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
    // ROLE_MANAGER)
    filter: 'member.type = "HUMAN"'
  };

  // Make the request
  const pageResult = chatClient.listMembershipsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

main().catch(console.error);

Python

chat/client-libraries/cloud/list_memberships_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 list memberships with app credential
def list_memberships_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.ListMembershipsRequest(
        # Replace SPACE_NAME here
        parent = 'spaces/SPACE_NAME',
        # Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
        # ROLE_MANAGER)
        filter = 'member.type = "HUMAN"',
        # Number of results that will be returned at once
        page_size = 100
    )

    # Make the request
    page_result = client.list_memberships(request)

    # Handle the response. Iterating over page_result will yield results and
    # resolve additional pages automatically.
    for response in page_result:
        print(response)

list_memberships_app_cred()

Java

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

// This sample shows how to list memberships with app credential.
public class ListMembershipsAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      ListMembershipsRequest.Builder request = ListMembershipsRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        // Filter membership by type (HUMAN or BOT) or role
        // (ROLE_MEMBER or ROLE_MANAGER).
        .setFilter("member.type = \"HUMAN\"")
        // Number of results that will be returned at once.
        .setPageSize(10);

      // Iterate over results and resolve additional pages automatically.
      for (Membership response :
          chatServiceClient.listMemberships(request.build()).iterateAll()) {
        System.out.println(JsonFormat.printer().print(response));
      }
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to list memberships with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function listMembershipsAppCred() {
// Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const parent = 'spaces/SPACE_NAME';
  // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
  // ROLE_MANAGER)
  const filter = 'member.type = "HUMAN"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.Members.list(parent, {
      filter: filter,
      pageSize: 10,
      pageToken: pageToken
    }, getHeaderWithAppCredentials());
    // Handle response pages
    if (responsePage.memberships) {
      responsePage.memberships.forEach((membership) => console.log(membership));
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

इस सैंपल को चलाने के लिए, SPACE_NAME को स्पेस के name फ़ील्ड से मिले आईडी से बदलें. आईडी पाने के लिए, ListSpaces() तरीके का इस्तेमाल करें या स्पेस के यूआरएल से आईडी पाएं.

Google Chat API, स्पेस में शामिल लोगों की सूची दिखाता है. इसमें स्पेस मैनेजर शामिल नहीं होते.

Google Workspace एडमिन के तौर पर सदस्यों की सूची बनाना

अगर आप Google Workspace एडमिन हैं, तो अपने Google Workspace संगठन के किसी भी स्पेस के लिए, सदस्यताएं देखने के लिए ListMemberships() तरीके का इस्तेमाल करें. Chat API सिर्फ़ आपके संगठन के उपयोगकर्ताओं (इंटरनल और बाहरी, दोनों) या Google ग्रुप की सदस्यताएं दिखाता है. इसलिए, यह किसी भी Chat ऐप्लिकेशन की सदस्यताएं नहीं दिखाता.

Google Workspace एडमिन के तौर पर इस तरीके को कॉल करने के लिए, यह तरीका अपनाएं:

  • उपयोगकर्ता की पुष्टि करने के लिए, इस तरीके का इस्तेमाल करें. साथ ही, अनुमति का दायरा तय करें. इससे एडमिन के अधिकारों का इस्तेमाल करके, इस तरीके को कॉल किया जा सकेगा.
  • अपने अनुरोध में, इन क्वेरी पैरामीटर के बारे में बताएं:
    • useAdminAccess को true पर सेट करें.
    • सिर्फ़ उपयोगकर्ताओं को वापस लाने के लिए, member.type के लिए filter को HUMAN के बराबर पर सेट करें.
    • उपयोगकर्ताओं और ग्रुप को वापस लाने के लिए, member.type के लिए filter को BOT के बराबर नहीं AND showGroups के बराबर true पर सेट करें.

ज़्यादा जानकारी और उदाहरणों के लिए, Google Workspace एडमिन के तौर पर Google Chat स्पेस मैनेज करना लेख पढ़ें.

पेज नंबर के हिसाब से बांटने की सुविधा को पसंद के मुताबिक बनाएं या सूची को फ़िल्टर करें

सदस्यताओं की सूची बनाने के लिए, यहां दिए गए क्वेरी पैरामीटर पास करें. इससे, सूची में शामिल सदस्यताओं के पेज नंबर को पसंद के मुताबिक बनाया जा सकता है या उन्हें फ़िल्टर किया जा सकता है:

  • pageSize: सदस्यताएं दिखाने की ज़्यादा से ज़्यादा संख्या. ऐसा हो सकता है कि सेवा इस वैल्यू से कम नतीजे दिखाए. यह जानकारी उपलब्ध न होने पर, ज़्यादा से ज़्यादा 100 स्पेस दिखाए जाते हैं. इसकी ज़्यादा से ज़्यादा वैल्यू 1,000 हो सकती है. अगर वैल्यू 1,000 से ज़्यादा है, तो उसे अपने-आप 1,000 में बदल दिया जाता है.
  • pageToken: यह एक पेज टोकन है, जो list spaces को कॉल करने पर मिलता है. अगला पेज पाने के लिए, यह टोकन दें. पेज नंबर बदलते समय, फ़िल्टर की वैल्यू उस कॉल से मेल खानी चाहिए जिसने पेज टोकन दिया था. अलग वैल्यू पास करने से, अनचाहे नतीजे मिल सकते हैं.
  • filter: क्वेरी फ़िल्टर. इसके लिए, उपयोगकर्ता की पुष्टि करना ज़रूरी है. क्वेरी की जानकारी के लिए, ListMembershipsRequest रेफ़रंस देखें.