איך מסירים אנשים ממרחבים משותפים

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

המשאב Membership מציין אם משתמש אנושי או אפליקציית Google Chat הוזמנו למרחב משותף, חלק ממנו או נעדרו ממנו.

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

Python

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

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

איך מסירים אנשים ממרחבים משותפים

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

  • כדי להסיר משתמש או קבוצה ב-Google, מציינים את היקף ההרשאה chat.memberships. כדי להסיר את אפליקציית Chat, צריך לציין את היקף ההרשאה chat.memberships.app (האפליקציות יכולות למחוק רק את המינוי שלהן, ולא את המינוי של אפליקציות אחרות). מומלץ לבחור את ההיקף המגביל ביותר שעדיין מאפשר לאפליקציה לפעול.
  • קוראים ל-method delete במשאב membership.
  • כדי למחוק את המינוי, צריך להעביר את name של המינוי. אם החברות היא רק למנהל/ת של המרחב המשותף, צריך להקצות משתמש אחר כמנהל של המרחב המשותף לפני מחיקת המינוי.

כך מוחקים מינוי:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_membership_delete.py.
  2. צריך לכלול את הקוד הבא ב-chat_membership_delete.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.memberships.app"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes the specified membership.
        '''
    
        # 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().members().delete(
    
            # The membership to delete.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBER with a membership name.
            # Obtain the membership name from the memberships resource of
            # Chat API. To delete a Chat app's membership, replace MEMBER
            # with app; an alias for the app calling the API.
            name='spaces/SPACE/members/MEMBER'
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        # When deleting a membership, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם המרחב, שאפשר לקבל באמצעות ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב.

    • MEMBER: שם המינוי, שאפשר לקבל באמצעות ה-method spaces.members.list ב-Chat API. כדי למחוק מינוי של אפליקציה, מחליפים את MEMBER ב-app.

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

    python3 chat_membership_delete.py
    

אם הפעולה בוצעה ללא שגיאות, גוף התשובה יחזיר את החברות עם 'state': 'NOT_A_MEMBER', כדי לציין שהחבר כבר לא במרחב המשותף.

{
    "name": "spaces/SPACE/members/MEMBER",
    "state": "NOT_A_MEMBER"
}