อัปเดตการเป็นสมาชิกของผู้ใช้ในพื้นที่ของ Google Chat

คู่มือนี้อธิบายวิธีใช้ update() เมธอดในแหล่งข้อมูล Membership ของ Google Chat API เพื่อเปลี่ยนแอตทริบิวต์ เกี่ยวกับการเป็นสมาชิก เช่น เปลี่ยนสมาชิกในพื้นที่ทำงานเป็นผู้จัดการพื้นที่ทำงานหรือ เจ้าของพื้นที่ทำงาน

หากคุณเป็นผู้ดูแลระบบ Google Workspace คุณสามารถเรียกเมธอด update() เพื่ออัปเดตการเป็นสมาชิกของพื้นที่ทำงานใดก็ได้ในองค์กร Google Workspace

แหล่งข้อมูล Membershipแสดงว่าผู้ใช้ที่เป็นบุคคลจริงหรือแอป Google Chat ได้รับเชิญ เข้าร่วม หรือไม่ได้อยู่ในพื้นที่ทำงาน

ข้อกำหนดเบื้องต้น

Node.js

อัปเดตการเป็นสมาชิก

หากต้องการอัปเดตการเป็นสมาชิกพื้นที่ทำงาน ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • ระบุขอบเขตการให้สิทธิ์ โดยทำดังนี้
    • หากใช้การตรวจสอบสิทธิ์ของผู้ใช้ ให้ ระบุขอบเขตการให้สิทธิ์ chat.memberships
    • หากใช้ การตรวจสอบสิทธิ์ของแอป, ให้ระบุขอบเขตการให้สิทธิ์ chat.app.memberships เมื่ออัปเดตการเป็นสมาชิกด้วยการตรวจสอบสิทธิ์ของแอป คุณจะอัปเดตได้เฉพาะการเป็นสมาชิกในพื้นที่ทำงานที่สร้างโดยแอป Chat การตรวจสอบสิทธิ์ของแอปต้องได้รับการอนุมัติจากผู้ดูแลระบบเพียงครั้งเดียว
  • เรียกเมธอด UpdateMembership()
  • ส่ง membership เป็นอินสแตนซ์ของ Membership พร้อมข้อมูลต่อไปนี้:
    • ช่อง name ตั้งค่าเป็นการเป็นสมาชิกที่จะอัปเดต ซึ่งรวมถึงรหัสพื้นที่ทำงานและรหัสสมาชิก
    • ช่องการเป็นสมาชิกที่จะอัปเดตตั้งค่าเป็นค่าใหม่
  • ส่ง updateMask เพื่อระบุลักษณะของการเป็นสมาชิกที่จะอัปเดต ซึ่งรวมถึงข้อมูลต่อไปนี้
    • role: บทบาทของผู้ใช้ในพื้นที่ทำงานของ Chat ซึ่งกำหนดการดำเนินการที่ได้รับอนุญาตในพื้นที่ทำงาน ดูคำอธิบายสิทธิ์โดยละเอียดได้ที่ MembershipRole ในเอกสารอ้างอิง Chat API ค่าที่เป็นไปได้มีดังนี้
      • ROLE_MEMBER: สมาชิกของพื้นที่ทำงาน ใน UI ของ Chat บทบาทนี้เรียกว่าสมาชิก
      • ROLE_ASSISTANT_MANAGER: ผู้จัดการพื้นที่ทำงาน ใน UI ของ Chat บทบาทนี้เรียกว่าผู้จัดการ
      • ROLE_MANAGER: เจ้าของพื้นที่ทำงาน ใน UI ของ Chat บทบาทนี้เรียกว่าเจ้าของ

เปลี่ยนสมาชิกเป็นเจ้าของ (การตรวจสอบสิทธิ์ของผู้ใช้)

ตัวอย่างต่อไปนี้เรียก Chat API โดยใช้ การตรวจสอบสิทธิ์ของผู้ใช้ เพื่อเปลี่ยนสมาชิกทั่วไปในพื้นที่ทำงานเป็นเจ้าของพื้นที่ทำงานโดยระบุ role เป็น ROLE_MANAGER

Node.js

chat/client-libraries/cloud/update-membership-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = [
  'https://www.googleapis.com/auth/chat.memberships',
];

// This sample shows how to update a membership with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    membership: {
      // Replace SPACE_NAME and MEMBER_NAME here
      name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
      // Replace ROLE_NAME here with ROLE_MEMBER or ROLE_MANAGER
      role: 'ROLE_NAME',
    },
    updateMask: {
      // The field paths to update.
      paths: ['role'],
    },
  };

  // Make the request
  const response = await chatClient.updateMembership(request);

  // Handle the response
  console.log(response);
}

await main();

หากต้องการเรียกใช้ตัวอย่าง ให้แทนที่ข้อมูลต่อไปนี้

  • SPACE_NAME: รหัสจาก name ของพื้นที่ทำงาน คุณดูรหัสได้โดยเรียก ListSpaces() เมธอด หรือจาก URL ของพื้นที่ทำงาน
  • MEMBER_NAME: รหัสจาก nameของการเป็นสมาชิก คุณดูรหัสได้โดยเรียก ListMemberships() เมธอด หรือจากเนื้อหาการตอบกลับที่แสดงขึ้นหลังจากสร้างการเป็นสมาชิก แบบไม่พร้อมกันด้วย Chat API
  • ROLE_NAME: บทบาทที่อัปเดต ROLE_MANAGER คุณตั้งค่านี้เป็นค่าใดก็ได้ของ MembershipRole. เช่น หากต้องการเปลี่ยนสมาชิกทั่วไปเป็นผู้จัดการพื้นที่ทำงาน ให้เปลี่ยน ROLE_NAME เป็น ROLE_ASSISTANT_MANAGER แทน

Google Chat API จะอัปเดตการเป็นสมาชิกที่ระบุเป็นเจ้าของพื้นที่ทำงานและแสดง อินสแตนซ์ของ Membership

เปลี่ยนเจ้าของเป็นสมาชิก (การตรวจสอบสิทธิ์ของผู้ใช้)

ตัวอย่างต่อไปนี้เรียก Chat API โดยใช้ การตรวจสอบสิทธิ์ของผู้ใช้ เพื่อเปลี่ยนเจ้าของพื้นที่ทำงานเป็นสมาชิกทั่วไปในพื้นที่ทำงานโดยระบุ role เป็น ROLE_MEMBER

Node.js

chat/client-libraries/cloud/update-membership-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = [
  'https://www.googleapis.com/auth/chat.memberships',
];

// This sample shows how to update a membership with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    membership: {
      // Replace SPACE_NAME and MEMBER_NAME here
      name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
      // Replace ROLE_NAME here with ROLE_MEMBER or ROLE_MANAGER
      role: 'ROLE_NAME',
    },
    updateMask: {
      // The field paths to update.
      paths: ['role'],
    },
  };

  // Make the request
  const response = await chatClient.updateMembership(request);

  // Handle the response
  console.log(response);
}

await main();

หากต้องการเรียกใช้ตัวอย่าง ให้แทนที่ข้อมูลต่อไปนี้

  • SPACE_NAME: รหัสจาก name ของพื้นที่ทำงาน คุณดูรหัสได้โดยเรียก ListSpaces() เมธอด หรือจาก URL ของพื้นที่ทำงาน
  • MEMBER_NAME: รหัสจาก nameของการเป็นสมาชิก คุณดูรหัสได้โดยเรียก ListMemberships() เมธอด หรือจากเนื้อหาการตอบกลับที่แสดงขึ้นหลังจากสร้างการเป็นสมาชิก แบบไม่พร้อมกันด้วย Chat API
  • ROLE_NAME: บทบาทที่อัปเดต ROLE_MEMBER

Google Chat API จะอัปเดตการเป็นสมาชิกที่ระบุเป็นเจ้าของพื้นที่ทำงานและแสดง อินสแตนซ์ของ Membership

เปลี่ยนสมาชิกเป็นเจ้าของ (การตรวจสอบสิทธิ์ของแอป Chat)

การตรวจสอบสิทธิ์ของแอปต้องได้รับการอนุมัติจากผู้ดูแลระบบเพียงครั้งเดียว

เขียนสคริปต์ที่เรียก Chat API

ตัวอย่างต่อไปนี้เรียก Chat API โดยใช้ การตรวจสอบสิทธิ์ของแอป เพื่อเปลี่ยนสมาชิกทั่วไปในพื้นที่ทำงานเป็นเจ้าของพื้นที่ทำงานโดยระบุ role เป็น ROLE_MANAGER ใน body ที่ระบุแอตทริบิวต์การเป็นสมาชิกที่อัปเดต

Python

  1. สร้างไฟล์ชื่อ chat_membership_update_to_owner_app.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_membership_update_to_owner_app.py

    from google.oauth2 import service_account
    from apiclient.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.app.memberships"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then updates a specified space member to change
        it from a regular member to a space owner.
        '''
    
        # Specify service account details.
        creds = (
            service_account.Credentials.from_service_account_file('credentials.json')
            .with_scopes(SCOPES)
        )
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
    
            # Replace ROLE with a MembershipRole value.
            # Obtain the MembershipRole values from the membership of Chat API.
            body={'role': 'ROLE'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ข้อมูลต่อไปนี้

    • SPACE: ชื่อพื้นที่ทำงาน ซึ่ง คุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBERSHIP: ชื่อการเป็นสมาชิก ซึ่ง คุณดูได้จาก spaces.members.list เมธอด ใน Chat API

    • ROLE: บทบาทที่อัปเดต ROLE_MANAGER คุณตั้งค่านี้เป็นค่าใดก็ได้ของ MembershipRole. เช่น หากต้องการเปลี่ยนสมาชิกทั่วไปเป็นผู้จัดการพื้นที่ทำงาน ให้เปลี่ยน ROLE เป็น ROLE_ASSISTANT_MANAGER แทน

  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีงานโดยทำดังนี้

    python3 chat_membership_update_to_owner_app.py

เปลี่ยนเจ้าของเป็นสมาชิก (การตรวจสอบสิทธิ์ของแอป Chat)

การตรวจสอบสิทธิ์ของแอปต้องได้รับการอนุมัติจากผู้ดูแลระบบเพียงครั้งเดียว

เขียนสคริปต์ที่เรียก Chat API

ตัวอย่างต่อไปนี้เรียก Chat API โดยใช้ การตรวจสอบสิทธิ์ของแอป เพื่อเปลี่ยนเจ้าของพื้นที่ทำงานเป็นสมาชิกทั่วไปในพื้นที่ทำงานโดยระบุ role เป็น ROLE_MEMBER ใน body ที่ระบุแอตทริบิวต์การเป็นสมาชิกที่อัปเดต

Python

  1. สร้างไฟล์ชื่อ chat_membership_update_to_member_app.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_membership_update_to_member_app.py

    from google.oauth2 import service_account
    from apiclient.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.app.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a specified space owner to change
        it to a regular member.
        '''
    
        # Specify service account details.
        creds = (
            service_account.Credentials.from_service_account_file('credentials.json')
            .with_scopes(SCOPES)
        )
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MEMBER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ข้อมูลต่อไปนี้

    • SPACE: ชื่อพื้นที่ทำงาน ซึ่ง คุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBERSHIP: ชื่อการเป็นสมาชิก ซึ่ง คุณดูได้จาก spaces.members.list เมธอด ใน Chat API

  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีงานโดยทำดังนี้

    python3 chat_membership_update_to_member_app.py

อัปเดตการเป็นสมาชิกในฐานะผู้ดูแลระบบ Google Workspace

หากคุณเป็นผู้ดูแลระบบ Google Workspace คุณสามารถเรียกเมธอด update() เพื่ออัปเดตการเป็นสมาชิกของพื้นที่ทำงานใดก็ได้ในองค์กร Google Workspace

หากต้องการเรียกเมธอดนี้ในฐานะผู้ดูแลระบบ Google Workspace ให้ทำดังนี้

ดูข้อมูลเพิ่มเติมและตัวอย่างได้ที่ จัดการพื้นที่ทำงานของ Google Chat ในฐานะผู้ดูแลระบบ Google Workspace