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

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

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

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

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

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

Python

  • Python 3.6 ומעלה
  • הכלי לניהול חבילות pip
  • ספריות הלקוח העדכניות של Google. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 ומעלה
  • ה-npm כלי לניהול חבילות
  • ספריות הלקוח העדכניות של Google. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:
    npm install @google-cloud/local-auth @googleapis/chat
    

רוצה להוסיף מרחב?

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

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

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

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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (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 בשם של מרחב משותף. שאפשר לקבל spaces.list() method ב 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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (spaces.list) מ-Chat API או מכתובת ה-URL של מרחב משותף.

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

    node app-get-space.js
    

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