Z tego przewodnika dowiesz się, jak używać metody patch
w zasobie membership
interfejsu Google Chat API do zmiany atrybutów subskrypcji,
użytkownika pokoju na menedżera pokoju lub zmianę menedżera pokoju na użytkownika pokoju.
Membership
zasób
określa zaproszenie użytkownika lub aplikacji Google Chat,
które znajdują się w pokoju lub w ogóle ich nie ma.
Wymagania wstępne
Python
- Firmy lub przedsiębiorstwa Konto Google Workspace z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth
- Włącz i skonfiguruj interfejs Google Chat API pod nazwą. ikonę i opis aplikacji Google Chat.
- Zainstaluj Python Biblioteka klienta interfejsów API Google.
-
Utwórz dane logowania na podstawie identyfikatora klienta OAuth dla aplikacji komputerowej. Aby uruchomić próbkę w tym
Przewodnika, zapisz dane logowania w pliku JSON o nazwie
client_secrets.json
katalog lokalny.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Node.js
- Firmy lub przedsiębiorstwa Konto Google Workspace z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth
- Włącz i skonfiguruj interfejs Google Chat API pod nazwą. ikonę i opis aplikacji Google Chat.
- Zainstaluj Node.js Biblioteka klienta interfejsów API Google.
-
Utwórz dane logowania na podstawie identyfikatora klienta OAuth dla aplikacji komputerowej. Aby uruchomić próbkę w tym
Przewodnika, zapisz dane logowania w pliku JSON o nazwie
client_secrets.json
katalog lokalny.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Google Apps Script
- Firmy lub przedsiębiorstwa Konto Google Workspace z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth
- Włącz i skonfiguruj interfejs Google Chat API pod nazwą. ikonę i opis aplikacji Google Chat.
- Utwórz samodzielny projekt Apps Script. i włącz zaawansowaną usługę czatu.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Aktualizowanie subskrypcji
Aby zaktualizować członkostwo w pokoju, w swojej prośbie podaj te informacje:
- Określ zakres autoryzacji
chat.memberships
. - Wywołaj funkcję
Metoda
patch
wMembership
zasobie, i przekazaćname
subskrypcji, aby zaktualizować, orazupdateMask
ibody
, który określa zaktualizowane atrybuty członkostwa. updateMask
określa aspekty subskrypcji do zaktualizowania. obejmuje następujące elementy:role
: rola użytkownika w pokoju czatu, która określa uprawnienia użytkownika. czynności w pokoju. Możliwe wartości to:ROLE_MEMBER
: osoba w pokoju. Użytkownik ma podstawowe uprawnienia, np. wysyłanie wiadomości do pokoju. W grupie 1:1 i bez nazwy rozmów, każdy ma tę rolę.ROLE_MANAGER
: menedżer pokoju. Użytkownik ma wszystkie podstawowe uprawnienia oraz uprawnienia administracyjne, które pozwalają im zarządzać pokojem, np. dodawać lub przez usunięcie członków. Obsługiwane tylko w pokojach, w którychspaceType
ma wartośćSPACE
(spacje z nazwami).
Ustawianie zwykłego członka pokoju jako menedżera pokoju
W przykładzie poniżej zwykły użytkownik pokoju staje się menedżerem pokoju przez określenie
role
jako ROLE_MANAGER
w elemencie body
, który określa zaktualizowaną subskrypcję
atrybuty:
Python
- W katalogu roboczym utwórz plik o nazwie
chat_membership_update.py
. Umieść w pliku
chat_membership_update.py
ten kod: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()
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, która który znajdziesz w Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.MEMBERSHIP
: nazwa subskrypcji, która który znajdziesz w Metodaspaces.members.list
w interfejsie Chat API.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_membership_update.py
Node.js
- W katalogu roboczym utwórz plik o nazwie
chat_membership_update.js
. Umieść w pliku
chat_membership_update.js
ten kod: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);
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, która który znajdziesz w Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.MEMBERSHIP
: nazwa subskrypcji, która który znajdziesz w Metodaspaces.members.list
w interfejsie Chat API.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_membership_update.js
Google Apps Script
W tym przykładzie wywołujemy interfejs Chat API za pomocą parametru Zaawansowana usługa czatu.
Dodaj zakres autoryzacji
chat.memberships
do Plikappsscript.json
projektu Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships" ]
Dodaj funkcję taką jak ta do kod:
/** * 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); } }
Interfejs Google Chat API zmienia określoną przynależność na menedżera pokoju i zwraca
wystąpienie instancji Membership
ze szczegółowymi informacjami na temat tej zmiany.
Ustawianie menedżera pokoju jako zwykłego użytkownika
W przykładzie poniżej menedżer pokoju staje się zwykłym członkiem pokoju przez określenie
role
jako ROLE_MEMBER
w elemencie body
, który określa zaktualizowaną subskrypcję
atrybuty:
Python
- W katalogu roboczym utwórz plik o nazwie
chat_membership_update.py
. Umieść w pliku
chat_membership_update.py
ten kod: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()
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, która który znajdziesz w Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.MEMBERSHIP
: nazwa subskrypcji, która który znajdziesz w Metodaspaces.members.list
w interfejsie Chat API.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_membership_update.py
Node.js
- W katalogu roboczym utwórz plik o nazwie
chat_membership_update.js
. Umieść w pliku
chat_membership_update.js
ten kod: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);
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, która który znajdziesz w Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.MEMBERSHIP
: nazwa subskrypcji, która który znajdziesz w Metodaspaces.members.list
w interfejsie Chat API.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_membership_update.js
Google Apps Script
W tym przykładzie wywołujemy interfejs Chat API za pomocą parametru Zaawansowana usługa czatu.
Dodaj zakres autoryzacji
chat.memberships
do Plikappsscript.json
projektu Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships" ]
Dodaj funkcję taką jak ta do kod:
/** * 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); } }
Interfejs Google Chat API zmienia określoną przynależność na menedżera pokoju i zwraca
wystąpienie instancji Membership
ze szczegółowymi informacjami na temat tej zmiany.
Powiązane artykuły
- Zapraszanie i dodawanie użytkowników lub aplikacji Google Chat do pokoju oraz dodawanie ich do pokoju
- Uzyskiwanie informacji o subskrypcji użytkownika lub w aplikacji Google Chat
- Wyświetlanie listy użytkowników w pokoju
- Usuwanie użytkownika lub aplikacji do obsługi czatu z pokoju