Hello, I’m trying to implement an OSLC consumer for Polarion without success.
I’m not sure if I should use Lyo 4.0.0 or Lyo 2.4.0. I tried both for now.
I have 2 issues. The first one is that a new token is generate when I call lookupServiceProviderUrl the second time.
The second issue is that I can’t have a successful authentication. I think I’m missing one step but I can’t figure out what. When I post with Postman to http://serveur/polarion/j_security_check, it works.
Here is my code, and the original post is here : Eclipse Community Forums: Lyo » Polarion oslc consumer oauth .
public class Main { private static final Logger logger = Logger.getLogger(Main.class.getName()); public static void main(String[] args) throws RootServicesException, IOException, OAuthException, URISyntaxException, ResourceNotFoundException, InterruptedException, InvalidCredentialsException { String webContextUrl = "http://serveur/polarion/oslc"; String securityUrl = "http://serveur/polarion/j_security_check"; String user = "admin"; String password = "psw"; String projectArea = "playground"; JazzRootServicesHelper helper = new JazzRootServicesHelper(webContextUrl, OSLCConstants.OSLC_RM_V2); HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(helper.getRequestConsumerKeyUrl())) .timeout(Duration.ofMinutes(1)) .header("Content-Type", "application/json") .POST(BodyPublishers.ofFile(Paths.get("D:\\Softwares\\eclipse 2019-03\\workspaceLyoClient\\lyo.client\\oslc-java-client\\src\\main\\java\\org\\eclipse\\lyo\\client\\test\\id.json"))) .build(); HttpResponse<String> responseKey = client.send(request, BodyHandlers.ofString()); if(responseKey.statusCode() == 200) { JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(responseKey.body()).getAsJsonObject(); String key = obj.get("key").getAsString(); System.out.println(key); OslcOAuthClient clientOslc = helper.initOAuthClient(key, "somesecret"); try { String res = clientOslc.lookupServiceProviderUrl(helper.getCatalogUrl(), projectArea); } catch (OAuthRedirectException oauthE) { HttpPost formPost = new HttpPost(securityUrl); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("j_username", user)); nvps.add(new BasicNameValuePair("j_password", password)); formPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); org.apache.http.HttpResponse formResponse = clientOslc.getHttpClient().execute(formPost); Header location = formResponse.getFirstHeader("Location"); System.out.println("Status : " + formResponse.getStatusLine().getStatusCode());//print 302 System.out.println("location : " + location); //print http://serveur/polarion HttpEntity entity = formResponse.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println("Body : " + responseString); //print blank EntityUtils.consume(formResponse.getEntity()); //Third GET HttpGet request4 = new HttpGet(location.getValue()); HttpClientParams.setRedirecting(request4.getParams(), false); org.apache.http.HttpResponse responseThird = clientOslc.getHttpClient().execute(request4); System.out.println("Status : " + responseThird.getStatusLine().getStatusCode()); //print 302 System.out.println("location : " + location); //print http://serveur/polarion entity = responseThird.getEntity(); responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println("Body : " + responseString); //print blank EntityUtils.consume(responseThird.getEntity()); //Since location is http://serveur/polarion this does not work /* Map<String,String> oAuthMap = getQueryMap(location.getValue()); System.out.println(oAuthMap.toString()); String oauthToken = oAuthMap.get("oauth_token"); String oauthverifier = oAuthMap.get("oauth_verifier");*/ // The server requires an authentication: Create the login form HttpPost formPost2 = new HttpPost(oauthE.getRedirectURL() + "?oauth_token=" + oauthE.getAccessor().requestToken); formPost2.getParams().setParameter("oauth_token", oauthE.getAccessor().requestToken); formPost2.getParams().setParameter("oauth_verifier", ""); formPost2.getParams().setParameter("authorize", "true"); formPost2.addHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); formResponse = clientOslc.getHttpClient().execute(formPost2); System.out.println("Status : " + formResponse.getStatusLine().getStatusCode()); System.out.println("location : " + location); entity = formResponse.getEntity(); responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println("Body : " + formResponse); EntityUtils.consume(formResponse.getEntity()); Header header = formResponse.getFirstHeader("Content-Length"); if ((header!=null) && (!("0".equals(header.getValue())))) { // The login failed throw new InvalidCredentialsException("Authentication failed"); } else { // The login succeed // Step (3): Request again the protected resource EntityUtils.consume(formResponse.getEntity()); String res = clientOslc.lookupServiceProviderUrl(helper.getCatalogUrl(), projectArea); System.out.println(res); } } } }