Invalid Token when using Auth0 ManagementAPI with Java

Hi there,

I am trying to use the management API with the java client library for java ( GitHub - auth0/auth0-java: Java client library for the Auth0 platform ), but when I call a method of ManagementAPI object I get an APIException with a invalid token description and an Unauthorized error.
To get my access token i followed this tutorial: Get Management API Access Tokens for Production .

 try {
        HttpResponse<String> response = Unirest.post("https://kevinkons.auth0.com/oauth/token")
                .header("content-type", "application/json")
                .body("{\"grant_type\":\"client_credentials\"," +
                        "\"client_id\": \"CEFmUinQ6ZanhgTyMMdTeCr5e46PZK87\"," +
                        "\"client_secret\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"," +
                        "\"audience\": \"https://kevinkons.auth0.com/api/v2/\"}")
                .asString();

        String accessTokenWithQuotationMarks = response.getBody().split(",")[0].split(":")[1];
        String accessToken = accessTokenWithQuotationMarks.substring(1, accessTokenWithQuotationMarks.length() -1);
        ManagementAPI mgmt = new ManagementAPI("https://kevinkons.auth0.com", accessToken);

        UserFilter filter = new UserFilter()
                .withPage(1, 20);

        Request<UsersPage> request = mgmt.users().list(filter);
        try {
            UsersPage responsee = request.execute();
            for(User u : responsee.getItems()) {
                u.getEmail();
            }
        } catch (APIException exception) {
            System.err.println("API " + exception.getDescription());
        } catch (Auth0Exception exception) {
            System.err.println("AUTH0" + exception.getMessage());
        }
    } catch (UnirestException e) {
        e.printStackTrace();
    }

{“access_token”:“eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik1VWXpRemsxTURBM09VWXhNekF3TkVZNU1FSXlORFZHTVRCR1JEUTRNakk0UTBaQ1JqUTJNQSJ9.eyJpc3MiOiJodHRwczovL2tldmlua29ucy5hdXRoMC5jb20vIiwic3ViIjoiQ0VGbVVpblE2WmFuaGdUeU1NZFRlQ3I1ZTQ2UFpLODdAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8va2V2aW5rb25zLmF1dGgwLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUxNDYzMjQ5LCJleHAiOjE1NTE1NDk2NDksImF6cCI6IkNFRm1VaW5RNlphbmhnVHlNTWRUZUNyNWU0NlBaSzg3IiwiZ3R5IjoiY2xpZW50LWNyZWRlbnRpYWxzIn0.vMnBFmLnJJHADAOW2fBL-KKd1dBNpnz8IBNuZ5B2jPDMMp_-EVplC7Ipj-IG0LzYNBQWj3bGwIOjxW6EbE7zhvnK8XM88daHIG9vTMQAVz45JFUgx3Cg732oDoOfl6vjoETLOp72n2km2VQvGEG-TIkyMdWgb8RouPwo6-0UKKa2co0pw_-TsR9ZPkGtlZ91IaPlno5m04ZREeiKbXqFeUCrcQ4uspq4JNN_ufLOQ3tR2UlbsTkFJV220jHFzqJTy7evur0eNW7anMvB6hFc70b_hzxlg70elGM_6nMujTJf3gv8ywpKc6oIfxlIuK-PL53NJT8rDCcspvAGaTYpVA”,“expires_in”:86400,“token_type”:“Bearer”}

This is the response I am receiving in the post to “https://kevinkons.auth0.com/oauth/token” according to the docs ( Get Management API Access Tokens for Production ) I should receive an scopes object but its not happening

I solved the problem by enabling the necessary scopes for my API Explorer Application. In the applications tab I selected the one created by clicking in the Create Authorize a Test Application button, then went to the API tab, clicked in the rigth arrow in the rigth side of the Auth0 Management API area and there i enabled the necessary scopes.

1 Like

Glad you were eventually able to make it!

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.