javasslsmartsheet-apismartsheet-java-sdk-v2

Error in connecting to Smartsheet using Java API


I am trying to connect to the smartsheet api using the java sdk they have provided. I am only a beginner in java and programming and completely new to smartsheet.

Here is my code (taken from http://smartsheet-platform.github.io/api-docs/?java#generating-access-token)

import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.models.enums.SourceInclusion;
import com.smartsheet.api.models.enums.ColumnType;
import com.smartsheet.api.oauth.*;

public class Test {
    public static void main(String[] args) throws SmartsheetException {
        SampleCode();
    }

    public static void SampleCode() throws SmartsheetException {
        // Set the Access Token
        Token token = new Token();
        token.setAccessToken("MY TOKEN");

        // Use the Smartsheet Builder to create a Smartsheet
        Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken(
                token.getAccessToken()).build();

        // Get current user.
        smartsheet.userResources().getCurrentUser();
    }

}

This is the error I am getting.

Exception in thread "main" com.smartsheet.api.internal.http.HttpClientException: Error occurred.
    at com.smartsheet.api.internal.http.DefaultHttpClient.request(DefaultHttpClient.java:169)
    at com.smartsheet.api.internal.AbstractResources.getResource(AbstractResources.java:192)
    at com.smartsheet.api.internal.UserResourcesImpl.getCurrentUser(UserResourcesImpl.java:179)
    at com.target.test.Test.SampleCode(Test.java:25)
    at com.target.test.Test.main(Test.java:12)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1506)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at com.smartsheet.api.internal.http.DefaultHttpClient.request(DefaultHttpClient.java:149)
    ... 4 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1488)
    ... 24 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:146)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 30 more

Solution

  • Error pretty much tells you about what is going wrong

    unable to find valid certification path to requested target
    

    The certificate chain returned by the server is not valid. This may happen due to variety of reasons, most likely due to a self-signed/expired/invalid certificate.

    You can check the certificates returned from the server using following linux command:

    openssl s_client -showcerts -connect yourserverUrl:port
    

    The problem most likely resides on the server side but for testing purposes you can workaround certificate validation error by adding the certs into your keystore as trused certs. Here is the command to add the cert to keystore:

    keytool -import -trustcacerts -file intermediate.crt -alias intermediateCA -keystore  $JAVA_HOME/jre/lib/security/cacerts
    

    As part of this import you will be asked to trust the cert, a message like this:

    Trust this certificate?
    

    Just enter yes and you should be good.