使用 Java API 连接到 Smartsheet 时出错

Error in connecting to Smartsheet using Java API

我正在尝试使用他们提供的 java SDK 连接到智能表 api。我只是 java 和编程的初学者,对 smartsheet 完全陌生。

这是我的代码(取自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();
    }

}

这是我遇到的错误。

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

错误几乎可以告诉您出了什么问题

unable to find valid certification path to requested target

服务器返回的证书链无效。这可能由于多种原因而发生,最有可能是由于 self-signed/expired/invalid 证书。

您可以使用以下 linux 命令检查从服务器返回的证书:

openssl s_client -showcerts -connect yourserverUrl:port

问题很可能出在服务器端,但出于测试目的,您可以通过将证书作为可信证书添加到密钥库中来解决证书验证错误。这是将证书添加到密钥库的命令:

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

作为此导入的一部分,您将被要求信任该证书,消息如下:

Trust this certificate?

只要输入yes就可以了。