0

I am new to OKTA. Using the below code to get the access token.. but getting 401 unauthorized error in this line

inputBuff = new BufferedReader(new InputStreamReader(httpsClient.getInputStream()));

String oktaURL = "https://xxx.oktapreview.com/oauth2/default/v1/token";
            String urlParameters = “client_id=” + clientId+“grant_type=authorization_code&redirect_uri=”+“http://:8192/app”+"&code="+oktaCode;
            URL url1 = new URL(oktaURL);
            StringBuffer response = null;
            String output1;

            log.info("The url to get the access token:"+url1.toString());
            if (url1.getProtocol() != null && url1.getProtocol().startsWith("https")){
                
                //String encodedData = DatatypeConverter.printBase64Binary((clientId + ":" + clientSecret).getBytes("UTF-8"));
                //String authorizationHeaderString = "Authorization: Basic " + encodedData;
                
                httpsClient = (HttpsURLConnection) url1.openConnection();
                httpsClient.setRequestMethod("POST");
                httpsClient.setRequestProperty("Accept","application/json");    
                httpsClient.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes()));
                httpsClient.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
                httpsClient.setInstanceFollowRedirects(false);
                
                log.info ("Send the POST request");
                // Send post request
                httpsClient.setDoOutput(true);
                try (DataOutputStream opStream = new DataOutputStream(httpsClient.getOutputStream())) {                     
                    opStream.writeBytes(urlParameters);
                    opStream.flush();
                }
                
                inputBuff = new BufferedReader(new InputStreamReader(httpsClient.getInputStream())); // throwing 401 here.
                log.info("Read from the input stream");

                response = new StringBuffer();
                while ((output1 = inputBuff.readLine()) != null) {
                    response.append(output1);
                }
            }

            if (response != null) {
                String theString = response.toString();
                log.trace("Info:"+theString);
            }

I could navigate to OKTA server's login page via /authorize URL and then authentication is successful and coming back to my application. Now trying to get access token. Please help how to solve this in java.

I just checked the okta log, it says the below right above the authenticate success log.

enter image description here

2 Answers 2

0

You forgot to put & after your actual client_id, so your string should look like

String urlParameters = 
    "client_id=" + clientId +
    "&grant_type=authorization_code" + 
    "&redirect_uri=" + "http://:8192/app" + 
    "&code=" + oktaCode;
0

Solve the issue, there were 2 issues.

  1. Removed client_id from urlParameters as its given in Authorization header
  2. Removed default from /token endpoint as its not give in my /authorize endpoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.