public Map<String, String> getParameters(GlobalWorldFacade world) { Map<String, String> parameters = new HashMap<>(); parameters.put("grant_type", name()); for (String attribute : requiredAttributes) { parameters.put(attributeNameMapping.get(attribute), world.getOrThrow(attribute)); } return parameters; } }
public Map<String, String> getOAuthResponse() throws IOException { String authServer = world.getOrThrow(OAuthCukesConstants.AUTH_SERVER); String clientId = world.getOrThrow(OAuthCukesConstants.CLIENT_ID); String clientSecret = world.getOrThrow(OAuthCukesConstants.CLIENT_SECRET); String grantType = world.getOrThrow(OAuthCukesConstants.GRANT_TYPE); URL url = new URL(authServer); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.addRequestProperty("Accept", "application/json"); connection.addRequestProperty("Authorization", "Basic " + Base64.encodeBase64String((clientId + ":" + clientSecret).getBytes())); connection.addRequestProperty("content-type", "application/x-www-form-urlencoded"); connection.setRequestMethod("POST"); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write("a=b" + getRequestParameter(grantType)); writer.flush(); int responseCode = connection.getResponseCode(); if (responseCode >= 400) { throw new IllegalStateException("Cannot retrieve OAuth token: " + IOUtils.toString(connection.getErrorStream())); } String response = IOUtils.toString(connection.getInputStream()); Type type = new TypeToken<Map<String, String>>() { }.getType(); return new Gson().fromJson(response, type); }