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

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

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

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

אימות באמצעות אימות אפליקציות מאפשר לאפליקציית Chat לקבל מרחבים משותפים שלאפליקציית Chat יש גישה אליהם ב-Google Chat (למשל, מרחבים שבהם האפליקציה חברה). אימות באמצעות אימות משתמשים מאפשר לכם לקבל מרחבים משותפים שלמשתמש המאומת יש גישה אליהם.

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

Python

  • Python 3.6 ואילך
  • הכלי pip לניהול חבילות
  • ספריות הלקוח העדכניות של Google ל-Python. כדי להתקין או לעדכן אותן, מריצים את הפקודה הבאה בממשק שורת הפקודה:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • פרויקט ב-Google Cloud עם ממשק Google Chat API פעיל ומוגדר. במאמר איך יוצרים אפליקציה ל-Google Chat מוסבר איך עושים זאת.
  • הוגדרה הרשאה לאפליקציית Chat. כשמגדירים מרחב משותף, אפשר להשתמש בשתי שיטות האימות הבאות:

Node.js

  • Node.js ו-npm
  • ספריות הלקוח העדכניות של Google ל-Node.js. כדי להתקין אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • פרויקט ב-Google Cloud עם ממשק Google Chat API פעיל ומוגדר. במאמר איך יוצרים אפליקציה ל-Google Chat מוסבר איך עושים זאת.
  • הוגדרה הרשאה לאפליקציית Chat. כשמגדירים מרחב משותף, אפשר להשתמש בשתי שיטות האימות הבאות:

להזמנת מרחב

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

  • באמצעות אימות אפליקציות, מציינים את היקף ההרשאה chat.bot. באמצעות אימות משתמשים, מציינים את היקף ההרשאה chat.spaces.readonly או chat.spaces.
  • מפעילים את ה-method get במשאב Space ומעבירים את name מהמרחב המשותף. לקבל את שם המרחב המשותף מהמשאב של Google Chat או מכתובת ה-URL של המרחב המשותף.

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

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

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_space_get_user.py.
  2. יש לכלול את הקוד הבא ב-chat_space_get_user.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets details about a specified space.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().get(
    
              # The space to get.
              #
              # Replace SPACE with a space name.
              # Obtain the space name from the spaces resource of Chat API,
              # or from a space's URL.
              name='spaces/SPACE'
    
          ).execute()
    
        # Prints details about the space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאותו אפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_space_get_user.py
    

Node.js

  1. בספריית העבודה, יוצרים קובץ בשם get-space.js.
  2. יש לכלול את הקוד הבא ב-get-space.js:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Gets details about a Chat space by name.
    * @return {!Object}
    */
    async function getSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.get({name: 'spaces/SPACE'});
    }
    
    getSpace().then(console.log);
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאותו אפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

  4. בספריית העבודה, מריצים את הדוגמה:

    node get-space.js
    

ה-Chat API מחזיר מופע של Space שכולל פרטים על המרחב המשותף.

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

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

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_space_get_app.py.
  2. יש לכלול את הקוד הבא ב-chat_space_get_app.py:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().get(
    
        # The space to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        name='spaces/SPACE'
    
    ).execute()
    
    print(result)
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאותו אפשר לקבל מה-method spaces.list() ב-Chat API או מכתובת ה-URL של המרחב המשותף.

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_space_get_app.py
    

Node.js

  1. בספריית העבודה, יוצרים קובץ בשם app-get-space.js.
  2. יש לכלול את הקוד הבא ב-app-get-space.js:

    const chat = require('@googleapis/chat');
    
    /**
    * Gets details about a Chat space by name.
    * @return {!Promise<!Object>}
    */
    async function getSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.get({name: 'spaces/SPACE'});
    }
    
    getSpace().then(console.log);
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאותו אפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

  4. בספריית העבודה, מריצים את הדוגמה:

    node app-get-space.js
    

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