นําผู้ใช้หรือแอป Google Chat ออกจากพื้นที่ทำงาน

คู่มือนี้อธิบายวิธีใช้เมธอด delete ในทรัพยากร membership ของ Google Chat API เพื่อนําผู้ใช้หรือแอป Chat ออกจากพื้นที่ทำงานหรือที่เรียกว่าการลบการเป็นสมาชิก ผู้จัดการพื้นที่ทำงานจะไม่สามารถนำผู้จัดการออกได้ หากเป็นผู้จัดการพื้นที่ทำงานเพียงคนเดียวในพื้นที่ทำงาน โปรดกำหนดให้ผู้ใช้รายอื่นเป็นผู้จัดการพื้นที่ทำงานก่อนจะนำการเป็นสมาชิกเหล่านี้ออก

แหล่งข้อมูลของ 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 จากผู้ใช้ที่มีสิทธิ์ลบการเป็นสมาชิกที่ระบุ

นําผู้ใช้หรือแอป Chat ออกจากพื้นที่ทำงาน

วิธีนําผู้ใช้หรือแอป Chat ออกจากพื้นที่ทำงาน

  • หากต้องการนำผู้ใช้ออก ให้ระบุขอบเขตการให้สิทธิ์ chat.memberships หากต้องการนำแอป Chat ออก ให้ระบุขอบเขตการให้สิทธิ์ chat.memberships.app รายการ (แอปจะลบได้เฉพาะการเป็นสมาชิกของตนเองเท่านั้น และไม่สามารถลบของแอปอื่นๆ ได้) แนวทางปฏิบัติแนะนำคือเลือกขอบเขตที่จำกัดที่สุดที่ยังทำให้แอปทำงานได้
  • เรียกเมธอด 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: ชื่อพื้นที่ทำงาน ซึ่งจะดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBER: ชื่อการเป็นสมาชิก ซึ่งคุณจะดูได้จากเมธอด 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"
}