# Okta access token using token endpoint url returns http 401 error

**URL:** <https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033>\
**Category:** Questions\
**Tags:** java\
**Created:** [November 13, 2021, 2:39am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033 "2021-11-13T02:39:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeline.alex](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@angeline.alex](https://devforum.okta.com/u/angeline.alex)\
**Post date:** [November 13, 2021, 2:39am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033/1 "2021-11-13T02:39:00Z")

</div>

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";
			
			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.

---

<div class="post-metadata">

**Author:** ![aaronpk](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/aaronpk/32/1737_2.png) [@aaronpk](https://devforum.okta.com/u/aaronpk)\
**Post date:** [November 13, 2021, 3:27am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033/2 "2021-11-13T03:27:16Z")

</div>

Where is your `urlParameters` defined? The error is likely because one or more parameters are missing.

---

<div class="post-metadata">

**Author:** ![angeline.alex](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@angeline.alex](https://devforum.okta.com/u/angeline.alex)\
**Post date:** [November 13, 2021, 3:38am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033/3 "2021-11-13T03:38:28Z")

</div>

just right above oktaURL

`String urlParameters = "client_id=" + clientId+"grant_type=authorization_code&redirect_uri="+"http://<host>:8192/app"+"&code="+oktaCode;`

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

![image](https://global.discourse-cdn.com/oktadev/original/2X/2/27acb0bb9bce20c4d8401faed674ec1d86e04d6a.png)

---

<div class="post-metadata">

**Author:** ![aaronpk](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/aaronpk/32/1737_2.png) [@aaronpk](https://devforum.okta.com/u/aaronpk)\
**Post date:** [November 13, 2021, 4:41am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033/4 "2021-11-13T04:41:22Z")

</div>

Got it, the `oktaUrl` looks like the first line I can see in the paste.

The error is accurate, you can only send the client id and secret in _either_ the post body or the HTTP header, so you’ll either need to remove the client\_id and client\_secret from the post body parameters or remove the Authorization header.

Also you’ll want to properly form-encode the post body parameters. I’d recommend using an HTTP client that has that built in rather than building the post body by hand.

---

<div class="post-metadata">

**Author:** ![angeline.alex](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@angeline.alex](https://devforum.okta.com/u/angeline.alex)\
**Post date:** [November 14, 2021, 6:38am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033/5 "2021-11-14T06:38:04Z")

</div>

I tried removing client\_id in either urlParameters or Autherization header; both returns 400 error.  
But i could see the below in the okta server log [instead of unknown client, its displaying app name [okta server) with mismatched\_authorization\_server]

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/f/f148d975a1428fdfdbe87d9f668565a41e374be2.png)

I also tried passing in the secret key in the parameter with client id and got 400 error.

Note:- My authorize endpoint and i got okta code and state.  
`https://xxx.oktapreview.com/oauth2/v1/authorize?client_id=067oa1tx8hesG8BlI1d7&response_type=code&response_mode=query&scope=openid&state=xyz&redirect_uri=http://<host>:8192/ng-prov`

Also can you give any reference URL/sample code for built in HttpClient.

UPDATE: Fixed the issue; getting the access\_token. Needed to either **remove**  **default** from / **token** endpoint or **add**  **default** in / **authorize** endpoint.

Thanks for all your inputs!!!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [November 15, 2021, 6:38am UTC](https://devforum.okta.com/t/okta-access-token-using-token-endpoint-url-returns-http-401-error/18033/6 "2021-11-15T06:38:34Z")

</div>

Closed on November 15, 2021, 6:38am UTC

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