Bir kullanıcının Google Chat alanındaki üyeliğini güncelleme

Bu rehberde, bir üyelikle ilgili özellikleri (ör. bir alan üyesini alan yöneticisi olarak değiştirme veya alan yöneticisini alan üyesi) değiştirmek için Google Chat API'nin membership kaynağındaki patch yönteminin nasıl kullanılacağı açıklanmaktadır.

Membership kaynağı, bir insan kullanıcının veya Google Chat uygulamasının bir alana davet edildiğini, bir alanın bir parçası olduğunu ya da bir alanda bulunmadığını belirtir.

Python

  • Python 3.6 veya sonraki sürümler
  • pip paket yönetimi aracı
  • Python için en yeni Google istemci kitaplıkları. Bunları yüklemek veya güncellemek için komut satırı arayüzünüzde aşağıdaki komutu çalıştırın:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Google Chat API'nin etkin ve yapılandırılmış olduğu bir Google Cloud projesi. Adımlar için Google Chat uygulaması oluşturma başlıklı makaleye göz atın.
  • Yetkilendirme, Chat uygulaması için yapılandırıldı. Üyeliğin güncellenmesi için chat.memberships yetkilendirme kapsamıyla veya Chat'e veri aktarıyorsanız chat.import yetkilendirme kapsamıyla Kullanıcı kimlik doğrulaması gerekir.

Node.js

  • Node.js ve npm
  • Node.js için en yeni Google istemci kitaplıkları. Bunları yüklemek için komut satırı arayüzünüzde aşağıdaki komutu çalıştırın:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • Google Chat API'nin etkin ve yapılandırılmış olduğu bir Google Cloud projesi. Adımlar için Google Chat uygulaması oluşturma başlıklı makaleye göz atın.
  • Yetkilendirme, Chat uygulaması için yapılandırıldı. Üyeliğin güncellenmesi için chat.memberships yetkilendirme kapsamıyla veya Chat'e veri aktarıyorsanız chat.import yetkilendirme kapsamıyla Kullanıcı kimlik doğrulaması gerekir.

Apps Komut Dosyası

Üyeliği güncelleme

Alan üyeliğini güncellemek için isteğinizde aşağıdakileri iletin:

  • chat.memberships yetkilendirme kapsamını belirtin.
  • Membership kaynağında patch yöntemini çağırın ve güncellenecek üyeliğin name öğesinin yanı sıra, güncellenen üyelik özelliklerini belirten updateMask ve body öğelerini iletin.
  • updateMask, güncellenecek üyeliğin özelliklerini belirtir ve aşağıdakileri içerir:
    • role: Kullanıcının Chat alanındaki rolü. Bu rol, kullanıcının alandaki izin verilen işlemlerini belirler. Olası değerler:
      • ROLE_MEMBER: Alanın bir üyesi. Kullanıcı, alana mesaj gönderme gibi temel izinlere sahip. Bire bir ve adsız grup görüşmelerinde herkes bu role sahiptir.
      • ROLE_MANAGER: Alan yöneticisi Kullanıcı, tüm temel izinlere ek olarak alanı yönetmesini sağlayan yönetim izinlerine (üye ekleme veya kaldırma gibi) sahiptir. Yalnızca spaceType değerinin SPACE (adlandırılmış alanlar) olduğu alanlarda desteklenir.

Normal bir alan üyesini alan yöneticisi yapma

Aşağıdaki örnekte, güncellenen üyelik özelliklerini belirten body içinde ROLE_MANAGER olarak role belirtilerek normal alan üyesi alan yöneticisi yapılır:

Python

  1. Çalışma dizininizde chat_membership_update.py adında bir dosya oluşturun.
  2. chat_membership_update.py içine şu kodu ekleyin:

    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()
    
  3. Kodda aşağıdakileri değiştirin:

  4. Çalışma dizininizde örneği derleyip çalıştırın:

    python3 chat_membership_update.py
    

Node.js

  1. Çalışma dizininizde chat_membership_update.js adında bir dosya oluşturun.
  2. chat_membership_update.js içine şu kodu ekleyin:

    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);
    
  3. Kodda aşağıdakileri değiştirin:

  4. Çalışma dizininizde örneği derleyip çalıştırın:

    python3 chat_membership_update.js
    

Apps Komut Dosyası

Bu örnek, Gelişmiş Sohbet Hizmeti'ni kullanan Chat API'yi çağırır.

  1. chat.memberships yetkilendirme kapsamını, Apps Komut Dosyası projesinin appsscript.json dosyasına ekleyin:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. Apps Komut Dosyası projesinin koduna şunun gibi bir işlev ekleyin:

    /**
     * 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);
      }
    }
    

Google Chat API, belirtilen üyeliği bir alan yöneticisi olarak değiştirir ve değişikliği ayrıntılı şekilde gösteren bir Membership örneğini döndürür.

Alan yöneticisini normal üye yapma

Aşağıdaki örnekte, güncellenen üyelik özelliklerini belirten body içinde role değerini ROLE_MEMBER olarak belirterek alan yöneticisini normal alan üyesi haline getirir:

Python

  1. Çalışma dizininizde chat_membership_update.py adında bir dosya oluşturun.
  2. chat_membership_update.py içine şu kodu ekleyin:

    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()
    
  3. Kodda aşağıdakileri değiştirin:

  4. Çalışma dizininizde örneği derleyip çalıştırın:

    python3 chat_membership_update.py
    

Node.js

  1. Çalışma dizininizde chat_membership_update.js adında bir dosya oluşturun.
  2. chat_membership_update.js içine şu kodu ekleyin:

    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);
    
  3. Kodda aşağıdakileri değiştirin:

  4. Çalışma dizininizde örneği derleyip çalıştırın:

    python3 chat_membership_update.js
    

Apps Komut Dosyası

Bu örnek, Gelişmiş Sohbet Hizmeti'ni kullanan Chat API'yi çağırır.

  1. chat.memberships yetkilendirme kapsamını, Apps Komut Dosyası projesinin appsscript.json dosyasına ekleyin:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. Apps Komut Dosyası projesinin koduna şunun gibi bir işlev ekleyin:

    /**
     * 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);
      }
    }
    

Google Chat API, belirtilen üyeliği bir alan yöneticisi olarak değiştirir ve değişikliği ayrıntılı şekilde gösteren bir Membership örneğini döndürür.