นำสมาชิกออกจากพื้นที่ทำงาน

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด 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 Group หรือ Chat ออกจากพื้นที่ทำงาน

  • หากต้องการนำผู้ใช้หรือ Google Group ออก ให้ระบุขอบเขตการให้สิทธิ์ 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"
}