איך מקבלים פרטים על מרחב משותף

במדריך הזה מוסבר איך להשתמש בשיטה get() במשאב Space של Google Chat API כדי לראות פרטים על מרחב, כמו השם המוצג, התיאור וההנחיות שלו.

אדמינים ב-Google Workspace יכולים להתקשר לשיטה get() כדי לאחזר פרטים על כל מרחב בארגון שלהם ב-Google Workspace.

משאב Space מייצג מקום שבו אנשים ואפליקציות ל-Chat יכולים לשלוח הודעות, לשתף קבצים ולשתף פעולה. יש כמה סוגים של מרחבים:

  • צ'אטים ישירים הם שיחות בין שני משתמשים או בין משתמש לבין אפליקציית Chat.
  • צ'אטים קבוצתיים הם שיחות בין שלושה משתמשים או יותר לבין אפליקציות ל-Chat.
  • מרחבים משותפים עם שם הם מקומות קבועים שבהם אנשים יכולים לשלוח הודעות, לשתף קבצים ולעבוד יחד.

אימות אפליקציות מאפשר לאפליקציות ל-Chat לקבל פרטים על מרחב שבו הן חברות. אימות באמצעות אימות משתמשים מאפשר לקבל מרחבים שלמשתמש המאומת יש גישה אליהם, כמשתתף במרחב או כאדמין ב-Google Workspace.

דרישות מוקדמות

Node.js

Python

Java

Apps Script

איך יוצרים מרחבים

כדי ליצור מרחב ב-Google Chat, צריך להעביר את הפרטים הבאים בבקשה:

  • היקף ההרשאה:
  • מבצעים קריאה ל-method‏ GetSpace() ומעבירים את name של המרחב שרוצים לאחזר. אפשר לקבל את שם המרחב מהמשאב Space של Google Chat או מכתובת ה-URL של המרחב.

איך מקבלים פרטים על המרחב כמשתמש

כך מקבלים את פרטי המרחב באמצעות אימות משתמש:

Node.js

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

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

// This sample shows how to get space 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
    name: 'spaces/SPACE_NAME',
  };

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

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

main().catch(console.error);

Python

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

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

    # Initialize request argument(s)
    request = google_chat.GetSpaceRequest(
        # Replace SPACE_NAME here
        name = "spaces/SPACE_NAME",
    )

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

    # Handle the response
    print(response)

get_space_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetSpaceUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetSpaceRequest;
import com.google.chat.v1.Space;

// This sample shows how to get space with user credential.
public class GetSpaceUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetSpaceRequest.Builder request = GetSpaceRequest.newBuilder()
        // Replace SPACE_NAME here
        .setName("spaces/SPACE_NAME");
      Space response = chatServiceClient.getSpace(request.build());

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

Apps Script

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

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

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

כדי להריץ את הדוגמה הזו, מחליפים את SPACE_NAME במזהה מהשדה name של המרחב. אפשר לקבל את המזהה על ידי הפעלת השיטה ListSpaces() או מכתובת ה-URL של המרחב.

ממשק Chat API מחזיר מופע של Space עם פרטים על המרחב שצוין.

קבלת פרטים על מרחב כאדמין ב-Google Workspace

אדמינים ב-Google Workspace יכולים להתקשר לשיטה GetSpace כדי לאחזר פרטים על כל מרחב בארגון Google Workspace שלהם.

כדי להפעיל את ה-method הזה כאדמינים ב-Google Workspace, צריך לבצע את הפעולות הבאות:

  • קוראים ל-method באמצעות אימות משתמש, ומציינים היקף הרשאות שתומך בקריאה ל-method באמצעות הרשאות אדמין.
  • בבקשה, מציינים את פרמטר השאילתה useAdminAccess כ-true.

מידע נוסף ודוגמאות זמינים במאמר ניהול מרחבים ב-Google Chat כאדמינים ב-Google Workspace.

קבלת פרטי המרחב כאפליקציית Chat

כך מקבלים את פרטי המרחב באמצעות אימות האפליקציה:

Node.js

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

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

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

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

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

main().catch(console.error);

Python

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

    # Initialize request argument(s)
    request = google_chat.GetSpaceRequest(
        # Replace SPACE_NAME here
        name = "spaces/SPACE_NAME",
    )

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

    # Handle the response
    print(response)

get_space_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetSpaceAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetSpaceRequest;
import com.google.chat.v1.Space;

// This sample shows how to get space with app credential.
public class GetSpaceAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetSpaceRequest.Builder request = GetSpaceRequest.newBuilder()
        // Replace SPACE_NAME here
        .setName("spaces/SPACE_NAME");
      Space response = chatServiceClient.getSpace(request.build());

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

Apps Script

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

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

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

כדי להריץ את הדוגמה הזו, מחליפים את SPACE_NAME במזהה מהשדה name של המרחב. אפשר לקבל את המזהה על ידי הפעלת השיטה ListSpaces() או מכתובת ה-URL של המרחב.

ממשק Chat API מחזיר מופע של Space עם פרטים על המרחב שצוין.

מגבלות ושיקולים