Skip to content
Advertisement

PKIX path building failed in OAuth Authentication in Java

I am trying to Authenticate in discogs:

https://www.discogs.com/developers/#page:authentication,header:authentication-oauth-flow

on the Point 2: SEND A GET REQUEST TO THE DISCOGS REQUEST TOKEN URL, I get this:

JavaScript

on the POINT 3: REDIRECT YOUR USER TO THE DISCOGS AUTHORIZE PAGE,

I’ve created this piece of code:

JavaScript

But I got this error:

but I get this error:

JavaScript

Advertisement

Answer

The error provided indicates that your application is unable to establish a SSL secure communication with the remote server, discogs.com, because it is unable to find a valid certificate for that server among the ones configured to trust.

Under the hood, HttpURLConnection will use Java Secure Socket Extension for establishing secure SSL communications.

In order to solve the problem you have several options, mainly:

  • Run your application with the javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword system properties pointing to a keystore and respectively password that contains your server certificates.
  • Include the server certificate in a trusted, default, keystore, one of:
    • <java-home>/lib/security/jssecacerts
    • <java-home>/lib/security/cacerts

No matter the chosen mechanism, be sure that the desired keystore contains all the necessary certificates to trust the remote server, not only the SSL certificate itself, but all the certificates in the certificate chain.

openssl provides an useful command that allows you to obtain all the certificates used in the SSL connection. In this case, it will provide the following information, among other:

JavaScript

In this case, it seems that the server is using a self-signed certificate to identify itself. You new to configure Java to trust that certificate.

Copy and paste the certificate text outputted in the command, including —–BEGIN CERTIFICATE—– and —–END CERTIFICATE—–, and save to a new file, discogs.pem, for example.

In order to be imported in a Java keystore, it should be converted to DER format first. You can do that with openssl as well:

JavaScript

Then, import the certificate in a keystore. As indicated, you can import it in the default JVM kystore, or in a new one. Let’s consider the later case and create a new keystore named, for example, discogs:

JavaScript

Provide a password of your convenience, let’s assume changeme for example.

Now, run the application with the above-mentioned system properties:

JavaScript

If necessary, in order to troubleshoot the problem, you can use the javax.net.debug system property with an appropriate value, all or ssl:

JavaScript

Please, consider review this related SO question, it may be of help.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement