Error while accessing to the Authorization Server

I receive an authorization code, now I want exchange this code for access and ID tokens, I pass it to my authorization server’s /token endpoint. but I receive this message

{“error_description”:“The grant was issued for another authorization server.”,“error”:“invalid_request”}

Anyone could help me ?

Below the code:


//Import required java libraries
import javax.servlet.
import javax.servlet.http.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;

//Extend HttpServlet class
public class callback extends HttpServlet {

private String message;

public void init() throws ServletException {
// Do required initialization
message = “Hello World”;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Set response content type
PrintWriter out = response.getWriter();

// Actual logic goes here.

Cookie[] cookieList=request.getCookies();

for(int i = 0; i < cookieList.length; i++)
System.out.println("Name: “+cookieList[i].getName()+ " value:”+ cookieList[i].getValue());


String queryString = null;
try {
String code=request.getParameter(“code”);

queryString = getTokenUri(code);
System.out.println("queryString: "+queryString);

} catch (UnsupportedEncodingException e) {

String clientId = “";
String clientSecret = "

String tokenEndpoint=“https://dev-******”;
byte[] encodedAuth = Base64.encodeBase64((clientId + “:” + clientSecret).getBytes());

CloseableHttpClient httpClient = HttpClients.custom()


HttpResponse jsonResponse = null;

try {
jsonResponse = + queryString)
.header(“user-agent”, null)
.header(“content-type”, “application/x-www-form-urlencoded”)
.header(“authorization”, "Basic " + new String(encodedAuth))
.header(“connection”, “close”)
.header(“accept”, “application/json”)
} catch (Exception e) {

   response.setHeader("WWW-Authenticate", "Bearer realm=\"Okta-Servlet-Example\"");
   response.sendError(401, "Unauthorized");


JsonNode tokens = jsonResponse.getBody();
for(int i = 0; i < tokens.getArray().length(); i++)
System.out.println("token.getId “+i+”: "+tokens.getArray().get(i));
String idToken = tokens.getObject().get(“id_token”).toString();


private String getTokenUri(String code) throws UnsupportedEncodingException {
String redirectUri = “http://localhost:8080/SurveyJSF/callback”;


return "grant_type=authorization_code&code=" +
        code +
        "&redirect_uri=" +redirectUri;


public static String convertToUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes(“UTF-8”), “ISO-8859-1”);
} catch ( e) {
return null;
return out;

public void destroy() {
// do nothing.

Did your authorization endpoint also have the same prefix or was it for a custom authorization server you created (if so replace default with the guid for that authz server)