I am trying to connect to a Rest API, and the code is as below,
public class TestingDevelop {
public static String fetchAccessToken(){
HttpClient httpclient = new DefaultHttpClient();
String client_id ="clientID";
String client_secret ="clientSecret";
String grant_type="client_credentials";
String accessTokenUrl = "tokenURL";
String scope = "scope";
HttpPost post = new HttpPost(accessTokenUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("client_id",client_id ));
params.add(new BasicNameValuePair("client_secret", client_secret));
params.add(new BasicNameValuePair("grant_type", grant_type));
params.add(new BasicNameValuePair("scope", scope));
post.setEntity(new UrlEncodedFormEntity(params));
System.out.println(post);
HttpResponse response = httpclient.execute(post);
String body = EntityUtils.toString(response.getEntity());
//the response is a json response and I am fetching the token from this.
return token;
}
}
I have used the generated token in postman and have checked, it works fine as expected.
Now, when I try to call the GET api, it throws
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Get API code:
private static void useBearerToken(String bearerToken) {
BufferedReader reader = null;
try {
String restApiUrl ="https://api.byu.edu:443/echo/v1/status?test=testing";
URL url = new URL(restApiUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + bearerToken);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
StringWriter out = new StringWriter(connection.getContentLength() > 0 ? connection.getContentLength() : 2048);
while ((line = reader.readLine()) != null) {
out.append(line);
}
String response = out.toString();
System.out.println(response);
} catch (Exception e) {
}
}
When, I look at the configuration at the postman side, the SSL is disabled.
I have seen similar question being asked but it did not work for me. Here are some of the links which I have gone through:
- Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error?
- Tried disabling the cert validation: SSLHandshakeException: ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
- PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException java
Updates
Postman console log:
tls: {…}
reused: false
authorized: false
authorizationError: "UNABLE_TO_VERIFY_LEAF_SIGNATURE"
Setting self signed certificate:
keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
setting system properties:
System.setProperty("javax.net.ssl.trustStore", "keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
and setting SSLContext
SSLContext.setDefault(
createSSLContext("/keystore.jks", "password"));
// call fetchAccessToken and useBearerToken()
The call fails before going to fetchAccessToken method.