Search code examples
agora.ioagora

[Agora]I want to create a chat user via RESTful API


I'm using Agora. I want to create a chat user via RESTful API. However, I am getting a 401 Unauthorized error. I don't think it's a token expiration error, but I don't understand "Registration is not open, please contact the app admin.".

Excerpt from the example

domain : Project List/Project Detail/Chat - Application Information - REST API appKey : Project List/Project Detail/Chat - Application Information - App Key

private String registerChatUser(String chatUserName) {
        String orgName = appKey.split("#")[0];
        String appName = appKey.split("#")[1];
        String url = "https://" + domain + "/" + orgName + "/" + appName + "/users";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        String token = getToken();
        headers.setBearerAuth(token);
        Map<String, String> body = new HashMap<>();
        body.put("username", chatUserName);
        body.put("password", "123");
        HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
        ResponseEntity<Map> response;
        System.out.println(entity);
        try {
            response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
        } catch (Exception e) {
            throw new RestClientException("register chat user error : " + e.getMessage());
        }
        List<Map<String, Object>> results = (List<Map<String, Object>>)    response.getBody().get("entities");
        return (String) results.get(0).get("uuid");
    }

and received register chat user error : 401 Unauthorized as a result


Solution

  • When facing "Registration is not open, please contact the app admin." means

    The error message returned because the app token is not included in the request header when registering users.

    Refer to agora->chat error code description here: https://docs.agora.io/en/agora-chat/reference/http-status-codes?platform=android#401-unauthorized

    And you can refer the "Secure authentication with tokens" doc to get the app token (a token with app privileges) here: https://docs.agora.io/en/agora-chat/develop/authentication?platform=android#call-agora-chat-restful-apis-with-tokens

    Hope this helps.