> ## Documentation Index
> Fetch the complete documentation index at: https://help.dingtalk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 创建、查询、修改及管理群会话

> 通过企业内部应用调用群会话相关接口实现群会话的创建、查询、修改与管理，含获取应用凭证、添加接口权限及获取 access_token 的完整流程。

本文档介绍了如何调用群会话相关接口实现群会话操作的相关流程。首先创建一个企业内部应用，再使用群会话提供的API，实现创建群会话、获取群会话信息、群会话管理操作、修改群会话流程。

## 流程简介

步骤一：获取应用 Client ID 和 Client Secret。

步骤三：获取应用访问凭证[获取企业内部应用的accessToken](/zh/open/development/obtain-the-access-token-of-an-internal-app)。

步骤四：调用服务端群会话相关API。

1. 调用服务端API-创建群接口，进行创建群会话，获取群会话`chatId`。
2. 根据群会话`chatId`，调用服务端API-[查询群信息](/zh/open/development/obtain-a-group-session)接口，获取群会话的信息。
3. 根据群会话chatId进行群会话管理操作。

   * 根据群会话`chatId`，调用新版服务端API-[获取群会话的OpenConversationId](/zh/open/development/obtain-group-openconversationid)接口，获取群会话`openConversationId`。

     * 根据群会话`openConversationId`，调用新版服务端API-批量设置企业群管理员接口，批量设置群管理员。
   * 根据群会话`chatId`，调用服务端API-更新群管理员接口，进行群会话管理员的设置。
   * 根据群会话`chatId`，调用服务端API-[设置禁止群成员私聊](/zh/open/development/set-private-chat)接口，设置群会话群成员间是否可以私聊。
   * 根据群会话`chatId`，调用服务端API-[更新群成员的群昵称](/zh/open/development/set-a-group-nickname)接口，进行设置群会话成员的昵称。
   * 根据群会话`chatId`，调用服务端API-[获取入群二维码链接](/zh/open/development/obtain-a-qr-code-link)接口，其他的企业成员点击链接即可申请加入群聊。
4. 根据群会话`chatId`，调用服务端API-更新群接口，进行群会话信息的修改。

## 前提条件

完成[应用创建与配置](/zh/open/dingstart/create-application)的流程。

## 步骤一：获取应用凭证

1. 选择目标应用，进入应用详情页，单击**基础信息** > **凭证与基础信息**。
2. 获取应用 Client ID 和 Client Secret。

## 步骤二：添加接口权限

单击**开发配置**>**权限管理**，在权限搜索框中分别输入`qyapi_chat_manage`、`qyapi_chat_read`和`qyapi_chat_base_read`，并申请权限。

## 步骤三：获取应用访问凭证accessToken

<Warning>
  服务端API差异详情参见旧版API VS 新版API。以下接口均使用新服务端API接口，SDK下载详情参见服务端SDK下载。
</Warning>

根据步骤一中的 Client ID 和 Client Secret，获取应用访问凭证[获取企业内部应用的accessToken](/zh/open/development/obtain-the-access-token-of-an-internal-app)。

```java theme={"theme":{"light":"github-light","dark":"github-dark"}}
 public void getAccessToken() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
        GetAccessTokenRequest accessTokenRequest = new GetAccessTokenRequest()
                .setAppKey("din*********hgn")
                .setAppSecret("9G_O************mBkhgGIO");
        GetAccessTokenResponse accessToken = client.getAccessToken(accessTokenRequest);
        System.out.println(JSON.toJSONString(accessToken.getBody()));
    }
```

## 步骤四：调用服务端群会话相关API

1. 调用服务端API-创建群接口，进行创建群会话，获取群会话chatId。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
   public void createChat() {
           DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/chat/create");
           OapiChatCreateRequest req = new OapiChatCreateRequest();
           req.setName("全员群");
           req.setOwner("manager4220");
           req.setUseridlist(Arrays.asList("userid1","userid2"));
           OapiChatCreateResponse rsp = client.execute(req, access_token);
           System.out.println(rsp);
       }
   ```
2. 根据群会话chatId，调用服务端API-[查询群信息](/zh/open/development/obtain-a-group-session)接口，获取群会话的信息。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
   public void getChatInfo() {
           DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/chat/get");
           OapiChatGetRequest req = new OapiChatGetRequest();
           req.setChatid("chate39f540dxxxx");
           req.setHttpMethod("GET");
           OapiChatGetResponse rsp = client.execute(req, access_token);
           System.out.println(rsp.getChatInfo());
       }
   ```
3. 根据群会话chatId进行群会话管理操作。

   * 根据群会话`chatId`，调用新版服务端API-[获取群会话的OpenConversationId](/zh/open/development/obtain-group-openconversationid)接口，获取群会话`openConversationId`。

     ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
     public void ConversationId() throws Exception {
             Config config = new Config();
             config.protocol = "https";
             config.regionId = "central";
             com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(config);
             ChatIdToOpenConversationIdHeaders chatIdToOpenConversationIdHeaders = new ChatIdToOpenConversationIdHeaders();
             chatIdToOpenConversationIdHeaders.xAcsDingtalkAccessToken = "accessToken";
             try {
                 ChatIdToOpenConversationIdResponse response = client.chatIdToOpenConversationIdWithOptions("chatId", chatIdToOpenConversationIdHeaders, new RuntimeOptions());
                 System.out.println(JSON.toJSONString(response.getBody()));
             } catch (TeaException err) {
                 if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                     // err 中含有 code 和 message 属性，可帮助开发定位问题
                     System.out.println(err.code);
                     System.out.println(err.message);
                 }

             } catch (Exception _err) {
                 TeaException err = new TeaException(_err.getMessage(), _err);
                 if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                     // err 中含有 code 和 message 属性，可帮助开发定位问题
                     System.out.println(err.code);
                     System.out.println(err.message);
                 }
             }
         }
     ```

     * 根据群会话`openConversationId`，调用新版服务端API-批量设置企业群管理员接口，批量设置群管理员。

       ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
       public void subAdministrators() throws Exception {
               Config config = new Config();
               config.protocol = "https";
               config.regionId = "central";
               com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(config);
               ChatSubAdminUpdateHeaders chatSubAdminUpdateHeaders = new ChatSubAdminUpdateHeaders();
               chatSubAdminUpdateHeaders.xAcsDingtalkAccessToken = "accessToken";
               ChatSubAdminUpdateRequest chatSubAdminUpdateRequest = new ChatSubAdminUpdateRequest()
                       .setOpenConversationId("cid55SzIIO********=")
                       .setUserIds(java.util.Arrays.asList(
                               "0147********7041"
                       ))
                       .setRole(2);
               try {
                   ChatSubAdminUpdateResponse chatSubAdminUpdateResponse = client.chatSubAdminUpdateWithOptions(chatSubAdminUpdateRequest, chatSubAdminUpdateHeaders, new RuntimeOptions());
                   System.out.println(JSON.toJSONString(chatSubAdminUpdateResponse.getBody()));
               } catch (TeaException err) {
                   if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                       // err 中含有 code 和 message 属性，可帮助开发定位问题
                       System.out.println(err.code);
                       System.out.println(err.message);
                   }
               } catch (Exception _err) {
                   TeaException err = new TeaException(_err.getMessage(), _err);
                   if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                       // err 中含有 code 和 message 属性，可帮助开发定位问题
                       System.out.println(err.code);
                       System.out.println(err.message);
                   }
               }
           }
       ```
   * 根据群会话`chatId`，调用服务端API-更新群管理员接口，进行设置群会话管理员。

     ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
     public void setChatAdmin() {
             DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.io/topapi/chat/subadmin/update");
             OapiChatSubadminUpdateRequest req = new OapiChatSubadminUpdateRequest();
             req.setChatid("chat14432xxxx");
             req.setUserids("userid1");
             req.setRole(2L);
             OapiChatSubadminUpdateResponse rsp = client.execute(req, access_token);
             System.out.println(rsp.getBody());
         }
     ```
   * 根据群会话`chatId`，调用服务端API-[设置禁止群成员私聊](/zh/open/development/set-private-chat)接口，设置群会话群成员间是否可以私聊。

     ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
     public void updateChatMemberFriendswitch() {
             DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.io/topapi/chat/member/friendswitch/update");
             OapiChatMemberFriendswitchUpdateRequest req = new OapiChatMemberFriendswitchUpdateRequest();
             req.setChatid("chatdafe234xxxx");
             req.setIsProhibit(true);
             OapiChatMemberFriendswitchUpdateResponse rsp = client.execute(req, access_token);
             System.out.println(rsp.getBody());
         }
     ```
   * 根据群会话`chatId`，调用服务端API-[更新群成员的群昵称](/zh/open/development/set-a-group-nickname)接口，进行设置群会话成员的昵称。

     ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
     public void updateChatMemberNick() {
             DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.io/topapi/chat/updategroupnick");
             OapiChatUpdategroupnickRequest req = new OapiChatUpdategroupnickRequest();
             req.setUserid("user123");
             req.setChatid("chate39f540d572b71cf97a556d95929fxxxx");
             req.setGroupNick("钉钉小二");
             OapiChatUpdategroupnickResponse rsp = client.execute(req, access_token);
             System.out.println(rsp.getBody());
         }
     ```
   * 根据群会话`chatId`，调用服务端API-[获取入群二维码链接](/zh/open/development/obtain-a-qr-code-link)接口，其他的企业成员点击链接即可申请加入群聊。

     ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
     public void getChatURL() {
             DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.io/topapi/chat/qrcode/get");
             OapiChatQrcodeGetRequest req = new OapiChatQrcodeGetRequest();
             req.setChatid("chat32****3a");
             req.setUserid("manager4220");
             OapiChatQrcodeGetResponse rsp = client.execute(req, access_token);
             System.out.println(rsp.getBody());
         }
     ```
4. 根据群会话`chatId`，调用服务端API-更新群接口，进行群会话信息的修改。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
   public void updateChat() {
           DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/chat/update");
           OapiChatUpdateRequest req = new OapiChatUpdateRequest();
           req.setChatid("chatxxxx");
           req.setName("群名称1");
           req.setOwner("04201724372xxxx");
           req.setOwnerType("emp");
           req.setAddUseridlist(Arrays.asList("userid2"));
           req.setDelUseridlist(Arrays.asList("userid1"));
           req.setIcon("@mediaId");
           req.setIsBan(true);
           req.setSearchable(0L);
           req.setValidationType(0L);
           req.setMentionAllAuthority(0L);
           req.setManagementType(0L);
           req.setChatBannedType(0L);
           req.setShowHistoryType(0L);
           OapiChatUpdateResponse rsp = client.execute(req,access_token);
           System.out.println(rsp.getBody());
       }
   ```
