Hướng dẫn này giải thích cách sử dụng phương thức patch
trên tài nguyên membership
của API Google Chat để thay đổi các thuộc tính về gói thành viên, chẳng hạn như thay đổi
thành viên không gian thành người quản lý không gian hoặc thay đổi người quản lý không gian thành thành viên không gian.
Chiến lược phát hành đĩa đơn
Tài nguyên Membership
cho biết liệu người dùng thực hoặc ứng dụng Google Chat có được mời tham gia hay không,
một phần hoặc không có trong không gian.
Điều kiện tiên quyết
Python
- Doanh nghiệp Tài khoản Google Workspace có quyền truy cập vào Google Chat.
- Thiết lập môi trường:
- Tạo một dự án trên Google Cloud.
- Định cấu hình màn hình xin phép bằng OAuth.
- Bật và định cấu hình API Google Chat bằng tên, biểu tượng và nội dung mô tả cho ứng dụng Chat.
- Cài đặt Python Thư viện ứng dụng API của Google.
-
Tạo thông tin xác thực mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong
hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên
client_secrets.json
vào thư mục nội bộ.
- Chọn phạm vi uỷ quyền có hỗ trợ việc xác thực người dùng.
Node.js
- Doanh nghiệp Tài khoản Google Workspace có quyền truy cập vào Google Chat.
- Thiết lập môi trường:
- Tạo một dự án trên Google Cloud.
- Định cấu hình màn hình xin phép bằng OAuth.
- Bật và định cấu hình API Google Chat bằng tên, biểu tượng và nội dung mô tả cho ứng dụng Chat.
- Cài đặt Node.js Thư viện ứng dụng API của Google.
-
Tạo thông tin xác thực mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong
hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên
client_secrets.json
vào thư mục nội bộ.
- Chọn phạm vi uỷ quyền có hỗ trợ việc xác thực người dùng.
Apps Script
- Doanh nghiệp Tài khoản Google Workspace có quyền truy cập vào Google Chat.
- Thiết lập môi trường:
- Tạo một dự án trên Google Cloud.
- Định cấu hình màn hình xin phép bằng OAuth.
- Bật và định cấu hình API Google Chat bằng tên, biểu tượng và nội dung mô tả cho ứng dụng Chat.
- Tạo một dự án Apps Script độc lập, rồi bật Dịch vụ trò chuyện nâng cao.
- Chọn phạm vi uỷ quyền có hỗ trợ việc xác thực người dùng.
Cập nhật gói thành viên
Để cập nhật tư cách thành viên của không gian, hãy truyền những thông tin sau vào yêu cầu:
- Chỉ định phạm vi uỷ quyền
chat.memberships
. - Gọi
Phương thức
patch
trên tài nguyênMembership
, và chuyểnname
của tư cách thành viên để cập nhật, cũng nhưupdateMask
vàbody
chỉ định các thuộc tính mới cập nhật của gói thành viên. updateMask
chỉ định các chương trình thành viên cần cập nhật, và bao gồm:role
: Vai trò của người dùng trong phòng Chat, vai trò này xác định những quyền mà họ được phép hành động trong không gian. Các giá trị có thể có là:ROLE_MEMBER
: Một thành viên của không gian. Người dùng có các quyền cơ bản, chẳng hạn như gửi tin nhắn đến không gian. Trong nhóm 1:1 và chưa đặt tên các cuộc trò chuyện, mọi người đều có vai trò này.ROLE_MANAGER
: Người quản lý không gian. Người dùng có tất cả các quyền cơ bản cộng thêm quyền quản trị cho phép họ quản lý không gian, chẳng hạn như thêm hoặc xoá thành viên. Chỉ được hỗ trợ trong những không gian cóspaceType
làSPACE
(không gian được đặt tên).
Chỉ định một thành viên bình thường của không gian làm người quản lý không gian
Ví dụ sau đây đưa một thành viên thông thường vào không gian trở thành người quản lý không gian bằng cách chỉ định
role
là ROLE_MANAGER
trong body
để chỉ định gói thành viên đã cập nhật
thuộc tính:
Python
- Trong thư mục đang làm việc, hãy tạo một tệp có tên
chat_membership_update.py
. Đưa mã sau vào
chat_membership_update.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"] def main(): ''' Authenticates with Chat API via user credentials, then updates a specified space member to change it from a regular member to a space manager. ''' # 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().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_MANAGER'} ).execute() # Prints details about the updated membership. print(result) if __name__ == '__main__': main()
Trong mã, thay thế các nội dung sau:
SPACE
: tên không gian mà bạn có thể lấy từ Phương thứcspaces.list
trong API Chat hoặc qua URL của một không gian.MEMBERSHIP
: tên thành viên mà bạn có thể lấy từ Phương thứcspaces.members.list
trong API Chat.
Trong thư mục đang làm việc, hãy tạo và chạy mẫu:
python3 chat_membership_update.py
Node.js
- Trong thư mục đang làm việc, hãy tạo một tệp có tên
chat_membership_update.js
. Đưa mã sau vào
chat_membership_update.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Updates a membership in a Chat space to change it from * a space member to a space manager. * @return {!Promise<!Object>} */ async function updateSpace() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.spaces.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', requestBody: { role: 'ROLE_MANAGER' } }); } /** * Use the service endpoint to call Chat API. */ updateSpace().then(console.log);
Trong mã, thay thế các nội dung sau:
SPACE
: tên không gian mà bạn có thể lấy từ Phương thứcspaces.list
trong API Chat hoặc qua URL của một không gian.MEMBERSHIP
: tên thành viên mà bạn có thể lấy từ Phương thứcspaces.members.list
trong API Chat.
Trong thư mục đang làm việc, hãy tạo và chạy mẫu:
python3 chat_membership_update.js
Apps Script
Ví dụ này gọi API Chat bằng cách sử dụng Dịch vụ trò chuyện nâng cao.
Thêm phạm vi uỷ quyền
chat.memberships
vào Tệpappsscript.json
của dự án Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships" ]
Thêm một hàm như hàm này vào dự án Apps Script mã:
/** * Updates a membership from space member to space manager. * @param {string} memberName The resource name of the membership. */ function updateMembershipToSpaceManager(memberName) { try { const body = {'role': 'ROLE_MANAGER'}; Chat.Spaces.Members.patch(memberName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to create message with error %s', err.message); } }
API Google Chat thay đổi thành viên đã chỉ định thành người quản lý không gian rồi trả về
một bản sao của Membership
nêu chi tiết thay đổi.
Chỉ định người quản lý không gian làm thành viên thường xuyên
Ví dụ sau đây đưa người quản lý không gian trở thành thành viên thông thường của không gian bằng cách chỉ định
role
là ROLE_MEMBER
trong body
để chỉ định gói thành viên đã cập nhật
thuộc tính:
Python
- Trong thư mục đang làm việc, hãy tạo một tệp có tên
chat_membership_update.py
. Đưa mã sau vào
chat_membership_update.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"] def main(): ''' Authenticates with Chat API via user credentials, then updates a specified space member to change it from a regular member to a space manager. ''' # 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().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()
Trong mã, thay thế các nội dung sau:
SPACE
: tên không gian mà bạn có thể lấy từ Phương thứcspaces.list
trong API Chat hoặc qua URL của một không gian.MEMBERSHIP
: tên thành viên mà bạn có thể lấy từ Phương thứcspaces.members.list
trong API Chat.
Trong thư mục đang làm việc, hãy tạo và chạy mẫu:
python3 chat_membership_update.py
Node.js
- Trong thư mục đang làm việc, hãy tạo một tệp có tên
chat_membership_update.js
. Đưa mã sau vào
chat_membership_update.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Updates a membership in a Chat space to change it from * a space manager to a space member. * @return {!Promise<!Object>} */ async function updateSpace() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.spaces.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', requestBody: { role: 'ROLE_MEMBER' } }); } /** * Use the service endpoint to call Chat API. */ updateSpace().then(console.log);
Trong mã, thay thế các nội dung sau:
SPACE
: tên không gian mà bạn có thể lấy từ Phương thứcspaces.list
trong API Chat hoặc qua URL của một không gian.MEMBERSHIP
: tên thành viên mà bạn có thể lấy từ Phương thứcspaces.members.list
trong API Chat.
Trong thư mục đang làm việc, hãy tạo và chạy mẫu:
python3 chat_membership_update.js
Apps Script
Ví dụ này gọi API Chat bằng cách sử dụng Dịch vụ trò chuyện nâng cao.
Thêm phạm vi uỷ quyền
chat.memberships
vào Tệpappsscript.json
của dự án Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships" ]
Thêm một hàm như hàm này vào dự án Apps Script mã:
/** * Updates a membership from space manager to space member. * @param {string} memberName The resource name of the membership. */ function updateMembershipToSpaceMember(memberName) { try { const body = {'role': 'ROLE_MEMBER'}; Chat.Spaces.Members.patch(memberName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to create message with error %s', err.message); } }
API Google Chat thay đổi thành viên đã chỉ định thành người quản lý không gian rồi trả về
một bản sao của Membership
nêu chi tiết thay đổi.
Chủ đề có liên quan
- Mời hoặc thêm người dùng hay ứng dụng Google Chat vào một không gian.
- Xem thông tin chi tiết về gói thành viên của một người dùng hoặc ứng dụng Chat.
- Liệt kê thành viên trong không gian.
- Xoá người dùng hoặc ứng dụng trong Chat khỏi không gian.