How to access user endpoint of cumulocity with REST call

Product/components used and version/fix level:

Cumulocity Production

Detailed explanation of the problem:

Hi, I have this microservice deployed on Cumulocity. I am calling REST Endpoints of Cumulocity to get all the users of the current Tenant, but I am getting unauthorized. Can you please tell me what am I doing wrong?

            String auth = tenantId + "/" + name + ":" + pass;
            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());

            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", encodedAuth);
            headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

            HttpEntity<String> entity = new HttpEntity<>(headers);

            int currentPage = 1;
            String path = String.format("/user/%s/users?currentPage=%s&pageSize=2000&withTotalPages=true", tenantId, currentPage);
            String url = getCurrentTenant() + path;

            logger.info("Fetching users for tenant {} , id {}, currentPage {}", getCurrentTenant(), tenantId, currentPage);

            ResponseEntity<Map> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    entity,
                    Map.class
            );

When this call gets hit, I get unauthorized, i tried without passing the tenant id in username but i get the same error. i am calling this endpoint from a different service in local, however i tried to call via postman as well but got same issues. This user has admin role in cumulocity.

I get this erorr: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: "{"error":"security/Unauthorized","message":"Full authentication is required to access this resource","info":"https://cumulocity.com/guides/users-guide/getting-started/"}"

Can you please help me understand the cause?

Thanks,
Samanyu

Your authorization header needs to be: Authorization: Basic {your base 64 auth-string}.

So try changing your 5th line to: headers.set("Authorization", "Basic " + encodedAuth);

3 Likes

Yeah just figured that out, such a dumb mistake. Also one more thing for people who might face these issues, make sure if you are trying to hit your endpoint via postman or any other microservice in local,

  1. make sure you pass the tenant id in user like tenantId/user this will be the user name
  2. You need to pass Basic, then it will work.