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.;
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”;
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.
}
}