# Error while accessing to the Authorization Server

**URL:** <https://devforum.okta.com/t/error-while-accessing-to-the-authorization-server/784>\
**Category:** Questions\
**Created:** [November 20, 2017, 3:24pm UTC](https://devforum.okta.com/t/error-while-accessing-to-the-authorization-server/784 "2017-11-20T15:24:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![askani71](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@askani71](https://devforum.okta.com/u/askani71)\
**Post date:** [November 20, 2017, 3:24pm UTC](https://devforum.okta.com/t/error-while-accessing-to-the-authorization-server/784/1 "2017-11-20T15:24:58Z")

</div>

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:

package it.is.survey.okta;

//Import required java libraries  
import [java.io](http://java.io)._;  
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  
response.setContentType(“text/html”);  
PrintWriter out = response.getWriter();

// Actual logic goes here.  
out.println(“message”);

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) {  
System.out.println(“Eccezione:”+e);  
}

String clientId = “**";  
String clientSecret = "**\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*”;

String tokenEndpoint=“https://dev-\*\*\*\*\*\*.oktapreview.com/oauth2/default/v1/token?”;  
byte[] encodedAuth = Base64.encodeBase64((clientId + “:” + clientSecret).getBytes());

CloseableHttpClient httpClient = HttpClients.custom()  
.disableCookieManagement()  
.build();

Unirest.setHttpClient(httpClient);

HttpResponse jsonResponse = null;

try {  
jsonResponse = Unirest.post(tokenEndpoint + 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”)  
.asJson();  
} catch (Exception e) {

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

```

}

JsonNode tokens = jsonResponse.getBody();  
System.out.println(“tokens:”+tokens);  
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();  
out.println(“idToken:”+idToken);

}

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

```
code=convertToUTF8(code);
redirectUri=convertToUTF8(redirectUri);

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 (java.io.UnsupportedEncodingException e) {  
return null;  
}  
return out;  
}

public void destroy() {  
// do nothing.  
}  
}

---

<div class="post-metadata">

**Author:** ![Govner](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/govner/32/1099_2.png) [@Govner](https://devforum.okta.com/u/Govner)\
**Post date:** [April 25, 2019, 9:20am UTC](https://devforum.okta.com/t/error-while-accessing-to-the-authorization-server/784/2 "2019-04-25T09:20:52Z")

</div>

> [@askani71](#):
>
> https://dev-\*\*\*\*\*\*.oktapreview.com/oauth2/default/v1/token?

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)

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [January 17, 2024, 11:24pm UTC](https://devforum.okta.com/t/error-while-accessing-to-the-authorization-server/784/3 "2024-01-17T23:24:39Z")

</div>

Closed on January 17, 2024, 11:24pm UTC


